【问题标题】:lateinitializationerror field imageFile has not been initialized后期初始化错误字段 imageFile 尚未初始化
【发布时间】:2021-09-25 06:12:34
【问题描述】:

我正在尝试从 Flutter Web 项目的图库中上传图片。但是,当我单击上传按钮时,我收到一个错误 "======== 小部件库捕获的异常 ==================== ===========================LateInitializationError: 字段 imageFile 尚未初始化。” 什么可能导致此错误?这是我的代码:

final picker = ImagePicker();
late File imageFile;

Future chooseImage(ImageSource source) async {
  final pickedFile = await picker.pickImage(source: source);
  setState(() {
    imageFile = File(pickedFile!.path);
  });
}

Container(
    child: imageFile != null ?
    Container(
      height: 200,
      width: 200,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: FileImage(imageFile),
        ),
        borderRadius: BorderRadius.circular(20)),
      padding: const EdgeInsets.all(15.0),
    ) :
    Padding(
      padding: const EdgeInsets.all(15.0),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(20.0),
          child: Image.asset(
            defaultPic,
            height: 250.0,
            width: 300.0,
            fit: BoxFit.cover,
          ),
        ),
    ),
  ),
  ElevatedButton(
    onPressed: () {
      chooseImage(ImageSource.gallery);
    },
    child: Text('Upload Picture'),
    style: ElevatedButton.styleFrom(
      primary: Colors.red,
      elevation: 3,
      shape: new RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(50.0),
      ),
    ),
  )

【问题讨论】:

    标签: flutter flutter-web flutter-image


    【解决方案1】:

    您确定 imageFile 已正确初始化吗?我认为问题在于,当您设置 imageFile 的值时,您将其设置为空。检查File(pickedFile!.path) 我认为这是空的。 1 最后一件事永远不要使用! 运算符,最好使用?。基本上你需要检查pickedFile.path != null

    【讨论】:

      【解决方案2】:

      我创建了一个 bool 变量来完成这项工作:

      PickedFile? pickedImage;
      late File imageFile;    
      bool _load = false;
      

      未来会是这样的:

          Future chooseImage(ImageSource source) async {
                final pickedFile = await picker.pickImage(source: source);
                setState(() {
                  imageFile = File(pickedFile!.path);
                  _load = false;
                });
              }
      

      并在容器中使用它:

      Container(
          child: _load == true ?
          Container(
            height: 200,
            width: 200,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: FileImage(imageFile),
              ),
              borderRadius: BorderRadius.circular(20)),
            padding: const EdgeInsets.all(15.0),
          ) :
          Padding(
            padding: const EdgeInsets.all(15.0),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(20.0),
                child: Image.asset(
                  defaultPic,
                  height: 250.0,
                  width: 300.0,
                  fit: BoxFit.cover,
                ),
              ),
          ),
        ),
        ElevatedButton(
          onPressed: () {
            chooseImage(ImageSource.gallery);
          },
          child: Text('Upload Picture'),
          style: ElevatedButton.styleFrom(
            primary: Colors.red,
            elevation: 3,
            shape: new RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(50.0),
            ),
          ),
        )
      

      【讨论】:

      • 嘿,你能帮我处理你的代码吗?我一直在用它来获取画廊上的图像,但没有显示任何错误,但是当我打开调试时,它显示了很多错误,这是图片imgur.com/a/B6Bm9Si
      猜你喜欢
      • 1970-01-01
      • 2022-07-27
      • 2021-10-27
      • 2021-12-29
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多