I am an absolute beginner at this, so please help. My goal here is to achieve to create a new document inside collection called sensors on Firestore, everytime a new user registers. I am able to get that done, but then I want to fetch field of the document with that specific user's userid only. I have tried following code as well but it gets me a list of all created documents, I only want a single particular document with particular userid.
The following solution I tried was from a youtuber TheNetNinja, but it gets entire list of all documents in my collection:
//sensor list from snapshot
List<Sensor> _sensorListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Sensor(
fireRead: doc.data['fireRead'] ?? false,
airQuality: doc.data['airQuality'] ?? 0,
carbonMonoxide: doc.data['carbonMonoxide'] ?? 0,
lpg: doc.data['lpg'] ?? 0,
smoke: doc.data['smoke'] ?? 0,
);
}).toList();
}
//get sensors stream
Stream<List<Sensor>> get sensors {
return sensorData.snapshots().map(_sensorListFromSnapshot);
}
So then I ended up trying follwing code just by guessing because I dont really understand these Streams- Providers and other classes of dart-flutter:
Sensor specificSensorFromSnapshot(DocumentSnapshot event) {
return Sensor(
fireRead: event.data['fireRead'] ?? false,
airQuality: event.data['airQuality'] ?? 0,
carbonMonoxide: event.data['carbonMonoxide'] ?? 0,
lpg: event.data['lpg'] ?? 0,
smoke: event.data['smoke'] ?? 0,
);
}
Stream<Sensor> get sensor {
final DocumentReference specificSensorData =
Firestore.instance.collection('sensors').document(uid);
return specificSensorData.snapshots().map(specificSensorFromSnapshot);
}
I have a model class called Sensor:
class Sensor {
final int airQuality;
final bool fireRead;
final int carbonMonoxide;
final int smoke;
final int lpg;
Sensor(
{this.fireRead,
this.smoke,
this.carbonMonoxide,
this.airQuality,
this.lpg});
}