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

问题描述 投票:0回答:1

我正在使用此library中的演示,效果很好。但是,当我在项目中实施时,在此行出现错误

错误:不是常量表达式。 constAssetImage(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
                                    )));
               },
       ),

这里是打印值

值是/storage/emulated/0/Android/data/xxx/files/Pictures/scaled_1572364487252xxxxxx.jpg

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

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

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

flutter dart assets photo
1个回答
0
投票

这里const AssetImage(snapshot.data[index]),您正在使用const构造函数。编译器期望编译时间常数,并且抱怨是因为您传入的参数不是常数,而是取决于snapshot.data的运行时值。

如果仅删除const关键字,则应正确编译。

© www.soinside.com 2019 - 2024. All rights reserved.