Home > Software design >  The property can't be unconditionally accessed because the receiver can be 'null'...?
The property can't be unconditionally accessed because the receiver can be 'null'...?

Time:06-13

Hey guys i have an error and the code is bellow:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

class ChatScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
            .snapshots(),
        builder:(ctx, snapshot){
          if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          final docs = snapshot.data.docs;
          return ListView.builder(
            itemCount: docs.length,
            itemBuilder: (ctx, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text(docs[index]['text']),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
          FirebaseFirestore.instance
              .collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
              .snapshots()
              .listen((event) {
            event.docs.forEach((element) {
              print(element['text']);
            });
          });
        },
      ),
    );
  }
}

Now the problem is in:

final docs = snapshot.data.docs;

And it says that:

The property 'docs' can't be unconditionally accessed because the receiver can be 
'null'.

it is just having an error in docs after the snapshot data so can anybody please help me in that?

Thanks.

CodePudding user response:

you are doing everything perfectly, except for the fact to add the Type for the StreamBuilder. Null safety alone won't solve your problem. Here is a piece of code that I altered a bit only in the body of Scaffold Widget.

StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
            .snapshots(),
        builder:(ctx, snapshot){
          if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          if(snapshot.hasData) {
            final docs = snapshot.data!.docs;
            return ListView.builder(
              itemCount: docs.length,
              itemBuilder: (ctx, index) => Container(
                padding: EdgeInsets.all(8),
                child: Text(docs[index]['text']),
              ),
            );
          }
          else {
            return Text("Something Went wrong");
          }

        },
      )

CodePudding user response:

As the error message says, The property docs can't be unconditionally accessed because the receiver can be null.

var docs = snapshot?.data?.docs;
          return ListView.builder(
            itemCount: docs?.length ?? 0,
            itemBuilder: (ctx, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text(docs?[index]['text'] ?? ''),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
         if(event.docs =! null) 
          FirebaseFirestore.instance
              .collection('chats/RMxQeDVKeYPOW940bWCH/messages/')
              .snapshots()
              .listen((event) {
            event.docs.forEach((element) {
              print(element['text']);
            });
          });
  • Related