The property 'docs' cannot be unconditionally accessed because received can be 'null' Flutter


After migrate to null-safety showing this error. What should I do now?

  Widget chatMessages() {
    return StreamBuilder(
        stream: messageStream,
        builder: (context, snapshot) {
          return snapshot.hasData
              ? ListView.builder(
                  padding: EdgeInsets.only(bottom: 70, top: 16),
                  reverse: true,
                  itemBuilder: (context, index) {
                    DocumentSnapshot ds =[index];
                    return chatMessageTitle(
                        ds["message"], myUserName == ds["sendBy"]);
              : Center(child: CircularProgressIndicator());

After adding null check (!) showing this error <the getter ‘docs’ is not defined for the type of object>

              reverse: true,
              itemBuilder: (context, index) {
                DocumentSnapshot ds =!.docs[index];


You have to cast to its type. Suppose the type is QuerySnapshot (change this with the actual type of

(! as QuerySnapshot).docs.length

Instead of typecasting at all locations, we can specify the type of stream in the StreamBuilder.


Now is inferred as QuerySnapshot and no typecast is required.!.docs.length

Answered By – Navaneeth P

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.