Cannot display any data from firestore flutter - firebase

i am developing a flutter mobile apps and i have encountered few error. which is when im running my apps, the data is not displayed. I try to retrieve the uid and specific field to be display. This is the code for the retrieve part
and this is my database where i want to retrieve the type of the user database.
This is the output from my apps output

Change your query to this.
firestore
.collection("User")
.where("uid", isEqualTo: result.id)
.where("type", isEqualTo: "teacher")
.get();

void getTeacherData(){
firestore
.collection("User")
.where("type", isEqualTo: "teacher")
.get().then((querySnapshot){
//Check here if not null
querySnapshot.docs.forEach((element){
print(element["name"]);
});
});
}
this will give you all the user who are teachers

According to this documentation, your query should be as follows:
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future getTeacherData() async {
firestore
.collection('User')
.where('type', isEqualTo: 'teacher')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result['uid']);
});
});
}
Please tell us if this worked for you.

Related

how to display documents firestoredatabase in home screen flutter

I want to display the documents in Firestore database in home screen by using snapshot
I need something like this
Text('documents["name"]')
Use this code:
List<String> _groupsId = [];
await fireStore
.collection('groups')
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
_groupsId.add(doc.id);
}
});
You will get all list key of documents

Update a field in the last document in Firestore collection Flutter

I am trying to update a field in the last document in the Firestore collection. My updating method is below:
updateHours() {
return usersRef.doc(firebaseAuth.currentUser!.uid).collection('posts')
.orderBy('datePublished', descending: true)
.limit(1).get().then((querySnapshot) {
return querySnapshot.docs.map((e) {
usersRef
.doc(firebaseAuth.currentUser!.uid).collection('posts')
.doc(e.reference.id)
.update({"totalTime": FieldValue.increment(1)});
});
});
}
This does not work. If I use .forEach(), then all documents get updated. So, how to update only the last document field?
To be able to update the totalTime field inside the last document, please use the following lines of code:
void updateHours() async{
CollectionReference postsRef = usersRef
.doc(firebaseAuth.currentUser!.uid)
.collection('posts');
QuerySnapshot query = await postsRef.orderBy('datePublished', descending: true)
.limit(1)
.getDocuments();
query.documents.forEach((doc) {
doc.reference.updateData({"totalTime": FieldValue.increment(1)});
});
}
Don't forget that Firebase APIs are asynchronous, and you need to wait for the data until it becomes available.

flutter firebase order by in a condition

the probleme in my code is whenever i add the orderby my code stops working and the data is not displaying even when i don't get any errors
here's the stream i sued :
Stream<QuerySnapshot> searchData(String textEntered) async* {
var _search = FirebaseFirestore.instance
.collection("users")
.doc(sharedPreferences!.getString("uid"))
.collection("inventaire")
.doc(widget.model!.InventoryID)
.collection("produits").where('BarCode', isGreaterThanOrEqualTo: textEntered).orderBy('LastUpdate', descending: true)
.snapshots();
yield* _search;
}
I'm guessing now, as there isn't much to go on. But you are probably missing an index for the query.
Check your logs if you get anything like: " The query requires an index."
You can read more about it at:
https://firebase.google.com/docs/firestore/query-data/indexing
Another alternative is using a StreamController to return a modified stream, that way you could refactor the code into a listen:
Stream<QuerySnapshot> searchData(String textEntered) {
var controller = StreamController<QuerySnapshot>();
FirebaseFirestore.instance
.collection("users")
.doc(sharedPreferences!.getString("uid"))
.collection("inventaire")
.doc(widget.model!.InventoryID)
.collection("produits").where('BarCode', isGreaterThanOrEqualTo: textEntered).orderBy('LastUpdate', descending: true)
.snapshots().listen((QuerySnapshot qSnapshot) {
controller.add(qSnapshot);
});
return controller.stream;
}
Using the StreamController could even allow you to map the documents out of the QuerySnapshot returned on the listen callback handler and instead returning a list of PODO objects already mapped as opposed to the QuerySnapshot. My two cents.

How to make a one-time simple query with Firebase Firestore?

In Dart/Flutter and learning Firebase Firestore... I'm using the following method to test before creating UI:
_testFireStore() async {
var result = Firestore.instance
.collection('users')
.where('uid', isEqualTo: 'IvBEiD990Vh0D9t24l2GCCdsrAf1')
.snapshots();
await for (var snapshot in result) {
for (var user in snapshot.documents) {
print('main.DEBUG: ' + user.data.toString());
}
}
}
It works as expected -- the print statement is executed initially, but also subsequently in real-time every time any field is updated in the document in the Firestore database.
How can this code be changed such that the snapshot is only retrieved once -- not "subscribed/listened" to... and thus we don't waste bandwidth on unwanted/unneeded data and the print statement is only executed once?
Firestore.instance.collection(...).where(...) returns a Query object. It has a method called getDocuments() that executes the query and gives you a Future with a single set of results.
var query = Firestore.instance
.collection('users')
.where('uid', isEqualTo: 'IvBEiD990Vh0D9t24l2GCCdsrAf1');
query.getDocuments().then((QuerySnapshot snapshot) {
// handle the results here
})
Or use await to get the QuerySnapshot, since getDocumets() returns a Future.
Use getDocuments(), to retrieve all the documents once:
_testFireStore() async {
var result = await Firestore.instance
.collection('users')
.where('uid', isEqualTo: 'IvBEiD990Vh0D9t24l2GCCdsrAf1')
.getDocuments();
print(result.documents.toString());
}

Flutter: Shows some Firestore Collection with a StreamBuilder from an authenticated Firebase Auth User

My app has a collection of cards and I need to add some car to the favorites list, and I would like that each user has its own favorites collection.
So I did the classic StreamBuilder sample to show some list on Flutter:
StreamBuilder<QuerySnapshot>(
stream: getCars()
If the getCars() function is like this, everything is ok:
getCars() {
return Firestore.instance
.collection("users")
.document("oT646MvXXXXXXXXXXXjlN8L1V2")
.collection("cars").snapshots();
}
Let's say that "oT646MvXXXXXXXXXXXjlN8L1V2" is the FirebaseUser uid.
But how can I read FirebaseUser uid dinamically to return the collection?
I tried this code but it didn't work, since it returns a Future:
getCarsError() async {
FirebaseUser fUser = await FirebaseAuth.instance.currentUser();
return Firestore.instance
.collection("users")
.document(fUser.uid)
.collection("cars").snapshots();
}
How can I acompplish that?
thank you
Okay, the idea is to create a stream (I use the rxDart library but you can make it without)
BehaviorSubject<Car> _carController = BehaviorSubject<Car>();
Function(Car) get pushCar => _carController.sink.add;
Stream<Car> get streamCar => _carController ;
...
StreamBuilder<Car>(
stream: streamCar
Then in your async function:
void getCars() async{
FirebaseUser fUser = await FirebaseAuth.instance.currentUser();
Firestore.instance
.collection("users")
.document(fUser.uid)
.collection("cars").getDocuments().then((querySnapshot){
for (document in querySnapshot.documents){
pushCar(Car.fromJson(document.data)); //Deserialize your car here
}).catchError((e)=>print(e)); //Do what you want to handle error
}
So you push asynchronously your car into your stream, and you just get the stream<Car>and print what you have to :)
Hope it's help !!

Resources