TypeError: Cannot read properties of undefined (reading 'STATE_CHANGED')

Viewed 367

When using firebase_storage in flutter, its working fine but when i try to listen to the progress when uploading a file to firebase using this code .snapshotEvents.listen. It gives me that error and the state is not updating.

Issue is in this line only:

task.snapshotEvents.listen((event) {

Here is the full code:

            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);
                  });
                });
              }

here is the error:

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

btw it works in older flutter versions. I dont know whats the issue with new version. Anyhelp will be appreciated. Thanks

2 Answers

I got same error, after updating firebase_storage to version 3.3.0. Seems like a bug in this version, didn't find pass around.

2022:
environment: sdk: ">=2.17.6 <3.0.0" firebase_storage: ^10.3.3


I received this error after adding additional file location subfolders to firebase storage in a .child reference.

For reference, refer to the example from flutterfire: https://github.com/firebase/flutterfire/blob/master/packages/firebase_storage/firebase_storage/example/lib/main.dart

TypeError: Cannot read properties of undefined (reading 'STATE_CHANGED')

is given in Future<UploadTask?> uploadFile in the Reference to the file:

ORIGINAL:

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

An ERROR occurs when adding a subfolder to the string:

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

SOLUTION add filePath reference as a string and add file.readAsBytes() to .putData for 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);
}

Regards,

Joshua

Related