Reputation: 95
The defined Stream<QuerySnapshot>
, i'm calling the uid from the class constructor
Stream<QuerySnapshot> get hotels {
// print("$uid in stream");
return ownerCollection.document(uid).collection("hotels").snapshots();
}
Setting the StreamProvider
class Hotels extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamProvider<QuerySnapshot>.value(
value: DatabaseService(uid: user.uid).hotels,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.orangeAccent,
title: Text("My Hotels"),
),
body: HotelList())
);
Here's my screen which is accessing the Stream<QuerySnapshot>
class HotelList extends StatefulWidget {
@override
_HotelListState createState() => _HotelListState();
}
class _HotelListState extends State<HotelList> {
@override
Widget build(BuildContext context) {
final hotels = Provider.of<QuerySnapshot>(context);
for(var doc in hotels.documents) {
print(doc.data);
}
return Container();
}
}
When i'm running it, it gives me the error: The getter 'documents' was called on null , Reciever : null
Please help, i wanna print the all documents from the subcollection "hotels".
Upvotes: 0
Views: 104
Reputation: 24781
As I said in the comment, the error message "X was called on null" means that X isn't what is null, but whatever you called it on. In your case, this is the problem code:
final hotels = Provider.of<QuerySnapshot>(context);
for(var doc in hotels.documents) {
print(doc.data);
}
The error is saying documents
is getting called on null, which means hotel
is null. That means that your provider fetching isn't working properly.
I haven't worked with the StreamProvider
much, but if I had to guess it's because the first time your widget is built the source stream hasn't published any data yet, which makes sense as the source of the stream is an online service that needs time to connect and gather the data. This means there is no QuerySnapshot
to return, and as such the Provider.of
is returning null.
Normally you would fix this by giving the StreamProvider
an initial value. Since the type is QuerySnapshot
, though, I'm not sure what a sensical initial value would be other than, well, null. In that case, your only real option is to just handle the case where hotel
is null.
final hotels = Provider.of<QuerySnapshot>(context);
if (hotels == null) {
print('There is no data yet.');
return Container();
}
...
Upvotes: 3