类型错误:无法读取未定义的属性(读取"STATE_CHANGED")



在flutter中使用firebase_storage时,它工作得很好,但是当我尝试在使用此代码. snapshotevents .listen上传文件到firebase时收听进度时。它给了我这个错误,状态没有更新。

问题只在这一行:

task.snapshotEvents.listen((事件){

下面是完整的代码:

FilePickerResult? result =
await FilePicker.platform.pickFiles();
if (result != null) {
Uint8List? file = result.files.first.bytes;
String fileName = result.files.first.name;
UploadTask task = FirebaseStorage.instance
.ref()
.child("files/$fileName")
.putData(file!);
task.snapshotEvents.listen((event) {
setState(() {
progress = ((event.bytesTransferred.toDouble() /
event.totalBytes.toDouble()) *
100)
.roundToDouble();
if (progress == 100) {
event.ref
.getDownloadURL()
.then((downloadUrl) => print(downloadUrl));
}
print(progress);
});
});
}

错误如下:

TypeError: Cannot read properties of undefined (reading 'STATE_CHANGED')
at startListen (http://localhost:7284/packages/firebase_storage_web/src/interop/storage.dart.lib.js:561:97)
at Object._runGuarded (http://localhost:7284/dart_sdk.js:40843:7)
at [_subscribe] (http://localhost:7284/dart_sdk.js:34405:17)
at [_createSubscription] (http://localhost:7284/dart_sdk.js:33673:46)
at _BroadcastStream.new.listen (http://localhost:7284/dart_sdk.js:33638:53)
at new _ForwardingStreamSubscription.new (http://localhost:7284/dart_sdk.js:38261:55)
at [_createSubscription] (http://localhost:7284/dart_sdk.js:38177:16)
at _MapStream.new.listen (http://localhost:7284/dart_sdk.js:38174:41)
at [_listenToStream] (http://localhost:7284/packages/async/src/stream_group.dart.lib.js:225:35)
at [_onListen] (http://localhost:7284/packages/async/src/stream_group.dart.lib.js:168:70)
at Object._runGuarded (http://localhost:7284/dart_sdk.js:40843:7)
at [_subscribe] (http://localhost:7284/dart_sdk.js:34405:17)
at [_createSubscription] (http://localhost:7284/dart_sdk.js:33673:46)
at _BroadcastStream.new.listen (http://localhost:7284/dart_sdk.js:33638:53)
at new _ForwardingStreamSubscription.new (http://localhost:7284/dart_sdk.js:38261:55)
at [_createSubscription] (http://localhost:7284/dart_sdk.js:38177:16)
at _HandleErrorStream.new.listen (http://localhost:7284/dart_sdk.js:38174:41)
at new _ForwardingStreamSubscription.new (http://localhost:7284/dart_sdk.js:38261:55)
at [_createSubscription] (http://localhost:7284/dart_sdk.js:38177:16)
at _MapStream.new.listen (http://localhost:7284/dart_sdk.js:38174:41)
at UplaodPage._UplaodPageState.new.<anonymous> (http://localhost:7284/packages/testerupload/UplaodPage.dart.lib.js:226:43)
at Generator.next (<anonymous>)
at http://localhost:7284/dart_sdk.js:40641:33
at _RootZone.runUnary (http://localhost:7284/dart_sdk.js:40511:59)
at _FutureListener.thenAwait.handleValue (http://localhost:7284/dart_sdk.js:35438:29)
at handleValueCallback (http://localhost:7284/dart_sdk.js:35999:49)
at _Future._propagateToListeners (http://localhost:7284/dart_sdk.js:36037:17)
at [_completeWithValue] (http://localhost:7284/dart_sdk.js:35872:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:7284/dart_sdk.js:35906:35)
at Object._microtaskLoop (http://localhost:7284/dart_sdk.js:40778:13)
at _startMicrotaskLoop (http://localhost:7284/dart_sdk.js:40784:13)
at http://localhost:7284/dart_sdk.js:36261:9

顺便说一句,它可以在旧的flutter版本中工作。我不知道新版本有什么问题。任何帮助都将不胜感激。由于

我得到了同样的错误,更新firebase_storage版本3.3.0后。好像是这个版本的bug,没有找到pass around

2022:
环境:Sdk: ">=2.17.6 " 3.0.0"firebase_storage: ^ 10.3.3


在.child引用中将额外的文件位置子文件夹添加到firebase存储后收到此错误。

参考,参考flutterfire中的示例:https://github.com/firebase/flutterfire/blob/master/packages/firebase_storage/firebase_storage/example/lib/main.dart

TypeError: Cannot read properties of undefinedSTATE_CHANGED)

Future<UploadTask?> uploadFile中给出,在文件的参考中:

原始:

// Create a Reference to the file
Reference ref = FirebaseStorage.instance
.ref()
.child('flutter-tests')
.child('/some-image.jpg');

向字符串中添加子文件夹时发生错误:

Reference ref = FirebaseStorage.instance
.ref()
.child('flutter-tests')
.child('/new-subfolder/some-image.jpg');

解决方案添加filePath引用作为字符串,并添加file.readAsBytes()到.putData为web:

final filePath = "/uploads/$folder/$name";
Reference ref = FirebaseStorage.instance.ref().child(filePath);
final metadata = SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': file.path},
);
if (kIsWeb) {
uploadTask = ref.putData(await file.readAsBytes(), metadata);
} else {
uploadTask = ref.putFile(io.File(file.path), metadata);
}

问候,约书亚

相关内容

  • 没有找到相关文章

最新更新