I have stored time stamp data in firebase, I am fetching it using streams and displaying it in the latest date order in a datatable widget. I used .orderBy to access descending bool. But neither true nor false worked, instead !snapshot.hasData condition is getting executed, Why orderBy is not working and what are the other queries that I can use to get the latest date order.
stream: FirebaseFirestore.instance
.collection('lender')
.doc(auth.currentUser!.email)
.collection('paymentData')
.where('name',
isEqualTo: Provider.of<UpdateNameProvider>(context,
listen: false)
.bname).orderBy('paidDate', descending: true)
.snapshots(),
//.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(
backgroundColor: Color(0xff8eacbb),
));
}
There are many possible reasons why a snapshot doesn't have data. At the very least you'll want to check if there's an error, and if so: log it somewhere.
Something like this:
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) { // 👈
return Text(snapshot.error); // 👈
}
...
Try like this.
FirebaseFirestore.instance
.collection('lender')
.doc(auth.currentUser!.email)
.collection('paymentData')
.orderBy('paidDate', descending: true).where('name',
isEqualTo: Provider.of<UpdateNameProvider>(context,
listen: false)
.bname)
.snapshots(),
Related
Example code
toys = toysRef.where('postState', isEqualTo: 'recruiting').orderBy('createdAt', descending: true).snapshots();
fruits = fruitsRef.where('postState', isEqualTo: 'recruiting').orderBy('createdAt', descending: true).snapshots();
hi I'm trying to combine or merge two streams
I want to show toys and fruits order by desc with one StreamBuilder Widget
There is a common field ['createdAt']
I want to implement like this.
if some item added to firestore , it should show on realtime.
You can use StreamGroup from rxdart to merge your toys and fruits.
StreamGroup<List<QuerySnapshot>> combinedStreams = StreamGroup<List<QuerySnapshot>>.merge([
toysRef.where('postState', isEqualTo: 'recruiting').orderBy('createdAt', descending: true).snapshots(),
fruitsRef.where('postState', isEqualTo: 'recruiting').orderBy('createdAt', descending: true).snapshots()
]);
and to finally have them sorted in a stream, you could have:
StreamBuilder<List<QuerySnapshot>>(
stream: combinedStreams,
builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshot) {
List<QueryDocumentSnapshot> combinedData = [];
for (QuerySnapshot snap in snapshot.data) {
combinedData.addAll(snap.docs);
}
combinedData.sort((a, b) => b['createdAt'].compareTo(a['createdAt']));
return ListView.builder(
itemCount: combinedData.length,
itemBuilder: (context, index) {
// Build your UI
}
);
}
)
You should use ListView.builder so that you have a performant list, and not render all the elements at once (would really lag your UI if you'd have a lot of elements).
Don't forget to check if snapshot has errors or is empty so that you can display a loader to the user or an error message.
I am trying to create a UDF for stream builder's stream value
this is my code without UDF
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection("chatrooms")
.snapshots(),
builder: (context, snapshot) {
now I want to change with udf but don't know what's return type should i take...
want to convert as following
child: StreamBuilder(
stream: myudf(),// this is what I want to create
builder: (context, snapshot) {
void myudf()//pls correct me ,udf type
{
//what return type should I apply for this myudf function and what to implement in body
FirebaseFirestore.instance
.collection("chatrooms")
.where("participants.${widget.usermodel.uid}", isEqualTo: true)
.snapshots(),
}
Try to use Stream return type
Stream myudf()//pls correct me ,udf type
{
//what return type should I apply for this myudf function and what to implement in body
return FirebaseFirestore.instance
.collection("chatrooms")
.where("participants.${widget.usermodel.uid}", isEqualTo: true)
.snapshots(),
}
I want to create a streambuilder to download multiple user-profiles from firebase. But to know which users are needed I have to get the user-ids at first, these are stored in an array. So I created a method to download the array and after this is done the streambuilder loads the user-data for each user-id from the array. Here's the code:
Method to get the array (executed in initState()):
Stream<QuerySnapshot> stream() async* {
job = Job.fromJson(await FirebaseFirestore.instance
.collection("jobs")
.doc(widget.jobId)
.get());
applicants = job.applicants;
await FirebaseFirestore.instance
.collection('users')
.where('uid', whereIn: applicants)
.snapshots();
}
And the streambuilder in the scaffolds' body:
body: isLoading
? Center(child: Container(child: CircularProgressIndicator()))
: applicants.isEmpty
? Center(
child: Text("no values"),
)
: StreamBuilder<QuerySnapshot>(
stream: stream(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else { xy }
So my question is if there's a possibility to combine the first method and the stream. Because at the moment the user can't get any update if an application is withdrawn while using the screen.
StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
FirestoreUser firestoreUser =
FirestoreUser.fromDocument(snapshot.data); // Here snapshot.data is retrieving QuerySnapshot.
// How I can convert it to DocumentSnapshot
...
Hello StackOverflow users
I need to give to my new firestoreUser variable, which type of DocumentSnapshot. But I can't get it.
After writing snapshot.data it gives me an error called "QuerySnapshot is not subtype of DocumentSnapshot"
P.s I'm using StreamBuilder as you can see. Thank you
Your stream is FirebaseFirestore.instance.collection('users').snapshots() which is a QuerySnapshot, meaning, a List of QueryDocumentSnapshot which Extends DocumentSnapshot.
So if you want the documentSnapshot of every users in your 'users' collection, you will have to iterate over snapshot.data.docs:
But if you want to get the document of a particular user, then you can do:
StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(userID).snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
FirestoreUser firestoreUser =
FirestoreUser.fromDocument(snapshot.data);
...
i am using nested stream and future to get 2 sets of data, but cannot sort the second set of data by time
Tried both but still not working.. why???
Error: Getter not found: 'Direction'.
.orderBy("time", Query.Direction.DESCENDING)
^^^^^^^^^
Error: Too many positional arguments: 1 allowed, but 2 found.
Try removing the extra positional arguments.
.orderBy("time", 'asc')
^
full code below:
return StreamBuilder(
stream: A,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
...
return
FutureBuilder(
future: FirebaseFirestore.instance
.collection("some path here")
.orderBy("time", Query.Direction.DESCENDING)
.get(),,
builder: (context, snapshot2) {
if (snapshot2.hasData) {
if (snapshot2.data!=null) {
return DisplayTile(
...
);
}
}
return CircularProgressIndicator();
}
);
})
: Container();
},
);
orderBy has 1 named parameter called descending of bool type to pass order.
Example from docs:
FirebaseFirestore.instance
.collection('users')
.orderBy('age', descending: true) // here
.get()
.then(...);