错误:不是常量表达式。(扑)

Ton*_*ony 8 assets photo dart flutter

我使用了这个库中的演示,它工作得很好。但是当我在我的项目中实施时,我在这一行出现错误

错误:不是常量表达式。const AssetImage(snapshot.data[index]),

我的Container正在包装中InkWell

  InkWell(
      child: Container(
                 padding:
                      EdgeInsets.zero,
                     height: 150,
                      width: 150,
                      decoration:
                            BoxDecoration(
                                      image: DecorationImage(
                                       image: AssetImage(snapshot.data[index]),
                                            fit: BoxFit .fill),
                                  ),
                           ),
      onTap: () {
               print('The value is ' + snapshot .data[index]);
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder:
                         (context) =>
                            const FullScreenWrapper(imageProvider:const AssetImage(snapshot.data[index]),  // here the error
                                    )));
               },
       ),
Run Code Online (Sandbox Code Playgroud)

这里的打印值

值为/storage/emulated/0/Android/data/xxx/files/Pictures/scaled_1572364487252xxx.jpg

如果我删除const,我会收到其他错误

常量创建的参数必须是常量表达式。尝试使参数成为有效的常量,或使用“new”来调用构造函数。

我什至尝试使用new但无济于事。

lyi*_*yio 20

在这里,const AssetImage(snapshot.data[index])您正在使用const构造函数。编译器需要一个编译时常量并且抱怨,因为你传入的参数不是常量而是依赖于snapshot.data.

如果您只是删除const关键字,它应该编译没有错误。

  • 我想你还必须删除“FullScreenWrapper”之前的“const”。 (2认同)
  • 我想我可能从某个地方复制了我的代码,但没有意识到它在我的一个小部件前面有一个领先的“const”关键字。删除它解决了问题。 (2认同)