简体   繁体   中英

How to solve ˜The getter 'documentID' was called on null.˜?

I have this function with scoped model in which I want to have a firebase increment to snapshot when the button is pressed, but It returns ˜The getter 'documentID' was called on null.˜, it changes the state of my icon but the number is not incremented. If I give the name of the document it works fine, but I don't want to specify it. Any thoughts on what could be the solution?

void main() {
runApp(EaiCasimiro());
}



class EaiCasimiro extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModel<LikesModel>(
    model: LikesModel(),
    child: MaterialApp(
      title: "E aí,  Casimiro?",
      home: HomeScreen(),
      debugShowCheckedModeBanner: false,
      )
    );
   }
}


class LikesModel extends Model {

DocumentSnapshot snapshot;




bool _liked = true;

static LikesModel of(BuildContext context) =>
  ScopedModel.of<LikesModel>(context, rebuildOnChange: true);


bool isLiked() => _liked;




void pressed(){
_liked = !_liked;
notifyListeners();
 }

void changeLikes() {
Firestore.instance
    .collection("lanchonetes")
    .document(snapshot.documentID)
    .updateData({'likes': FieldValue.increment(_liked ? -1 : 1)});


    }

}

Since you have a lot of code here, but haven't noted a specific line where the error occurs, I'll guess it happens here:

Firestore.instance
    .collection("lanchonetes")
    .document(snapshot.documentID)
    .updateData({'likes': FieldValue.increment(_liked ? -1 : 1)});

It took me a while to find where you defined snapshot , but I found it here:

DocumentSnapshot snapshot;

Since you didn't assign a value when you declared it, it's going to have an initial value of null. So, when you use it in the query like this: snapshot.documentID , you're getting that error.

Make sure snapshot is defined before you use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM