Error: Not a constant expression in Flutter
Error: Not a constant expression in Flutter
我使用了这个 library 中的演示,并且效果很好。
但是当我在我的项目中实现时,我在这一行出现错误
Error: Not a constant expression. 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
)));
},
),
这里是打印值
The value is
/storage/emulated/0/Android/data/xxx/files/Pictures/scaled_1572364487252xxx.jpg
如果我删除 const,我会收到其他错误
Arguments of a constant creation must be constant expressions. Try
making the argument a valid constant, or use 'new' to call the
constructor.
我什至尝试使用 new
但无济于事。
此处 const AssetImage(snapshot.data[index])
您使用的是 const
构造函数。编译器期望编译时间常量并抱怨,因为您传入的参数不是常量,而是取决于 snapshot.data
.
的运行时值
如果您简单地删除 const
关键字,它应该可以正确编译。
我使用了这个 library 中的演示,并且效果很好。 但是当我在我的项目中实现时,我在这一行出现错误
Error: Not a constant expression. 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
)));
},
),
这里是打印值
The value is /storage/emulated/0/Android/data/xxx/files/Pictures/scaled_1572364487252xxx.jpg
如果我删除 const,我会收到其他错误
Arguments of a constant creation must be constant expressions. Try making the argument a valid constant, or use 'new' to call the constructor.
我什至尝试使用 new
但无济于事。
此处 const AssetImage(snapshot.data[index])
您使用的是 const
构造函数。编译器期望编译时间常量并抱怨,因为您传入的参数不是常量,而是取决于 snapshot.data
.
如果您简单地删除 const
关键字,它应该可以正确编译。