在Firebase云存储上上传图像时出错:

时间:2020-11-06 14:29:36

标签: firebase flutter dart firebase-storage

以下代码似乎可以毫无问题地上传我使用图像选择器拾取的图像。但是仍然会在调试控制台中引发此错误。请帮我解决。我是使用Firebase存储的新手,因此非常感谢您提供有关正确功能的说明!

代码:

Future uploadFile() async {
    String random = randomString(8);
    String out = 'images/' + random;
    Reference storageReference = FirebaseStorage.instance
        .ref()
        .child(out);
    UploadTask uploadTask = storageReference.putFile(_image);
    await uploadTask.onComplete;
    print('file uploaded');

    await storageReference.getDownloadURL().then((fileURL) {
      setState(() {
        this._uploadFileURL = fileURL;
      });
    }).whenComplete(() async {
      await imgColRef.add({'url': _uploadFileURL});
      print('link added to database');
    });
  }
}

错误:

E/flutter (29955): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: setState() called after dispose(): _AddDocumentState#deefa(lifecycle state: defunct, not mounted)
E/flutter (29955): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (29955): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (29955): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

randomString方法来自软件包https://pub.dev/packages/random_string

0 个答案:

没有答案
相关问题