Home > other >  Error: The method '[]' can't be unconditionally invoked because the receiver can be &
Error: The method '[]' can't be unconditionally invoked because the receiver can be &

Time:12-28

i'm beginning to practicing flutter todo app crud with a youtube guide video, this is the code:

enter image description here

but my code blows this error into [ ]:

The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
dart(unchecked_use_of_nullable_value)

enter image description here

I also tried the suggestion to fix it with a "!", but it still wrong.

enter image description here

I also tried adding an "?" to fix it, bit it still wrong

The receiver can't be null, so the null-aware operator '?.' is unnecessary.
Try replacing the operator '?.' with '.'.dart(invalid_null_aware_operator)
The getter '[' isn't defined for the type 'DataSnapshot'.
Try importing the library that defines '[', correcting the name to the name of an existing getter, or defining a getter or field named '['.dart(undefined_getter)
Expected an identifier.dart(missing_identifier)

enter image description here

CodePudding user response:

I think the problem is cause in the video are using an old version of Firestore

Try changing to this:

Course. fromSnapShot (DataSnapshot snapshot){
   Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;

   _id = snapshot.key;
   name = data["name"];
   description = data["description"l:
   url = data["url"];
   valoration = data["valoration"]:
   imgUrl = data["imgurl"];
}

CodePudding user response:

I dont know why, but i updated my dependecys just like that, and the eror was solved for this moment. So strange...

  cloud_firestore: ^3.1.5
  cupertino_icons: ^1.0.2
  firebase_auth: ^1.0.4
  firebase_core: ^1.4.0
  firebase_database: ^6.0.0 

or dependencis below null safety

  cloud_firestore: ^0.16.0
  cupertino_icons: ^1.0.2
  firebase_auth: ^0.20.1
  firebase_core: ^1.0.1
  firebase_database: ^6.0.0 
  • Related