0

I am trying to read data from Firebase Firestore, but I am not able to do so, and I have some Errors. I did it before this way and it worked but now I think the way to read data is changed.

The Error:

The property 'docs' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

Adding a null check does not solve the problem. Firestore has no rules to block the data.

lass Toyota extends StatefulWidget {
  const Toyota({Key? key}) : super(key: key);

  @override
  State<Toyota> createState() => _ToyotaState();
}

class _ToyotaState extends State<Toyota> {
  final firestore = FirebaseFirestore.instance.collection("toyota").get();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('singers').snapshots(),
          builder: (context, snapshot) {
            return ListView.builder(
              itemCount: (snapshot.data as QuerySnapshot).docs.length,
              itemBuilder: (context, index) =>  Text(
                           ////// Here is the problem ( The docs )
                            snapshot.data.docs[index]["price"],
                           
                          ),
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mt Khalifa
  • 459
  • 1
  • 6
  • 20

3 Answers3

0

(snapshot.data! as QuerySnapshot).docs.length

write like this at itemcount instead of it

(snapshot.data as QuerySnapshot).docs.length,

if still any confusion then ask me

Jay Limbani
  • 134
  • 5
  • The getter 'length' isn't defined for the type 'List> Function()'. Try importing the library that defines 'length', correcting the name to the name of an existing getter, or defining a getter or field named 'length'. – Mt Khalifa Jun 29 '22 at 12:35
  • You have to write length() like this not length only – Jay Limbani Jun 30 '22 at 13:09
0

You need to put a condition for when your snapshot has data

here is an example:

if (snapshot.hasData) {
          return ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(snapshot.data.docs[index].data()['name']),
                  subtitle: Text(snapshot.data.docs[index].data()['age']),
                );
              });
        } else {
          return Center(
            child: CircularProgressIndicator(),
          );
        }
mike-g
  • 1
  • 1
  • ``` The getter 'docs' isn't defined for the type 'Object'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'.``` – Mt Khalifa Jun 29 '22 at 12:38
  • still it does not recognize – Mt Khalifa Jun 29 '22 at 12:39
  • make sure you specify the type... so if you're using a FutureBuilder or a StreamBuilder you can specify the type like this: `FutureBuilder` – mike-g Jun 30 '22 at 13:54
  • the problem is it doesn't get the ( length) ... what type should i write there? – Mt Khalifa Jun 30 '22 at 14:10
  • either a string and list would support length property so my guess is you have to specify the type to be `` or `List` where `T` is the type of data in the list. – mike-g Jun 30 '22 at 14:34
0
static final FirebaseFirestore _firestore = FirebaseFirestore.instance;

QuerySnapshot snapshot = await _firestore
        .collection('toyota')
        .get();

Check this out.

MD Sarfaraj
  • 89
  • 2
  • 8