Reputation: 213
I'm having issues with getting messages fetched from Firebase Cloud Firestore. The error being displayed is: 'Error: The getter 'docs' isn't defined for the class 'Object' - 'Object' is from 'dart:core'.'
Below is my code:
class ChatScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<Object>(
stream: FirebaseFirestore.instance
.collection('chats/EKLJIb8ZfRoDTqxkkJaB/messages')
.snapshots(),
builder: (context, chatSnapshot) {
return ListView.builder(
itemCount: chatSnapshot.data.**docs**.length,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8),
child: Text('this work'),
),
);
}),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: null,
),
);
}
}
Upvotes: 2
Views: 2273
Reputation: 80914
Change this:
body: StreamBuilder<Object>(
into this:
body: StreamBuilder<QuerySnapshot>(
The docs
is a property under the class QuerySnapshot
and not class Object
.
You have to specify the type that your stream
returns. So if you have a stream
of type Stream<QuerySnapshot>
, then you add that type as an argument to the StreamBuilder
as you have seen in the code above.
In the new cloud_firestore
package, the snapshots()
method returns Stream<QuerySnapshot<Map<String, dynamic>>>
therefore you would do:
body: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
Adding dynamic
might solve this issue, but it's preferable to add the type that is being returned because that way the editor or IDE will provide you with the code completion.
Upvotes: 6
Reputation: 1
I have solved it by adding the datatype as dynamic after StreamBuilder, in my case the answer "Peter Haddad" didn't work for me, I had the exact same error.
This is how I solved it:
StreamBuilder<dynamic>(....Your Code here....)
Upvotes: 0