代码之家  ›  专栏  ›  技术社区  ›  Tony

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

  •  0
  • Tony  · 技术社区  · 6 年前

    我用这个演示 library 而且效果很好。 但是当我在我的项目中实现时,我在这条线上得到了错误

    错误:不是常量表达式。常量 AssetImage(快照数据[索引]),

    我的 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
                                        )));
                   },
           ),
    

    这里是印刷值

    价值是 /存储/模拟/0/Android/data/xxx/files/Pictures/scaled_1572364487252xxx.jpg

    如果我移除 常量 ,我得到其他错误

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

    我甚至试着用 new 但没用。

    1 回复  |  直到 6 年前
        1
  •  0
  •   lyio    6 年前

    在这里 const AssetImage(snapshot.data[index]) 你正在使用 const 构造器。编译器需要一个编译时常量,但由于传入的参数不是常量,而是取决于 snapshot.data .

    如果你只是把 常量 它应该编译没有错误的关键字。