Home > Net >  The property 'documents' cannot be unconditionally accessed because receiver can be '
The property 'documents' cannot be unconditionally accessed because receiver can be '

Time:10-10

I have been trying to make a todo app where I have used snapshot but I'm getting error for documents.it says The property 'documents' cannot be unconditionally accessed because receiver can be 'null'

code :

  body: Container(
    padding: EdgeInsets.all(10),
    height: MediaQuery.of(context).size.height,
    width: MediaQuery.of(context).size.width,
    child: StreamBuilder(
      stream: Firestore.instance
          .collection('tasks')
          .document(uid)
          .collection('mytasks')
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
          final docs = snapshot.data.documents; //facing error

          

I have also tried final docs = snapshot.data?.documents; it give error-the getter document isn't defined for the type "object"

enter image description here

CodePudding user response:

I think you are using firebase plugin after 1.0.0 so updated code is

final docs = snapshot.data!.docs;

CodePudding user response:

simply add "!" and specify the type of snapshot in builder params:-

builder: (context,AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
          final docs = snapshot.data!.docs;
  • Related