【问题标题】:Flutter : delete pick file image after picked itFlutter:选择后删除选择文件图像
【发布时间】:2020-12-24 12:53:55
【问题描述】:

我想在选择后删除图像,但执行此操作后会显示此错误,并且图像容器返回错误屏幕

'package:flutter/src/painting/image_provider.dart':断言失败: 第 854 行 pos 14: 'file != null': is not true.

这是我用来删除图像并显示图像的方法

Container(
        height: MediaQuery.of(context).size.height * 0.2, //list height
        child: ListView.builder(
          itemCount: uploadedFilesList.length,
          shrinkWrap: true,
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                child: Column(
                  children: [
                    InkWell(
                        onTap: () {
                          setState(() {
                            uploadedFilesList[index] = null;
                          });
                        },
                        child: Icon(Icons.close)),
                    Image.file(
                      uploadedFilesList[index],
                      width: MediaQuery.of(context).size.width * .25,
                      height: MediaQuery.of(context).size.height * .2,
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),

谁能告诉我我的代码哪里出了问题?

【问题讨论】:

    标签: flutter dart imagepicker


    【解决方案1】:

    在您的 setState 中,您正在设置 UploadedFilesList[index] = null;这意味着您尝试显示的列表中有一个空值:

    Image.file(uploadedFilesList[index], ....)
    

    所以基本上,错误是说您将 null 传递给图像提供者。

    要修复它,我认为您应该使用以下方法删除该索引处的元素:

    setState(() => uploadedFilesList.removeAt(index));
    

    而不是将其设置为 null。

    【讨论】:

    • 你是对的,真的感谢你拯救了我的一天,祝你一切顺利
    【解决方案2】:
    setState(() {
         uploadedFilesList[index] = null;
         });
    

    这不应该为空

    将 null 替换为任何虚拟 Image 或将 Image 提供程序替换为空容器。 您不能为 Image 提供程序提供空值。

    uploadedFilesList[index]==null?Image.file(
               uploadedFilesList[index],
               width: MediaQuery.of(context).size.width * .25,
               height: MediaQuery.of(context).size.height * .2,
                        ):Container(
               width: MediaQuery.of(context).size.width * .25,
               height: MediaQuery.of(context).size.height * .2,),
    

    【讨论】:

      猜你喜欢
      • 2018-03-14
      • 2012-06-26
      • 2022-08-19
      • 2021-11-19
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 2020-09-18
      • 2016-09-13
      相关资源
      最近更新 更多