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(...);
Related
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 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(),
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 want a ListView to show the names of the users. I am using a cloudfunction with the admin sdk to return a list of all the users with the corresponding user IDs. When I want to pass that uid to a Widget with a streambuilder, it gives me the error:
Class 'QuerySnapshot' has no instance method '[]'.
Receiver: Instance of 'QuerySnapshot'
Tried calling: []("firstName")
This is the function I am calling while building the ListView for the title:
Widget getFirstName(uid, item) {
return StreamBuilder(
stream: Firestore.instance
.collection('users')
.document('HzBUMs06BAPHK0Y7m5kfOmUzawC2')
.collection('userInfo')
.snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Text('${item['email']}');
} else {
return Text('${snapshot.data.documents['firstName']}');
}
},
);
}
I am not using the uid which I will pass to it yet, as the User ID that I hardcoded right now is the only one with the firstName data in it.
When I feed it a non-existing userID, it still seems to think it has data in it and tries to return its (non-existent) data.
What am I doing wrong here?
I managed to fix it by using this piece of code:
Widget fullNameWidget(uid) {
return FutureBuilder(
future: fullName(uid),
builder: (context, snapshot) {
return Text('${snapshot.data}');
},
);
}
Future fullName(uid) async {
return Firestore.instance
.collection("users")
.document('$uid')
.collection("userInfo")
.getDocuments()
.then((querySnapshot) {
print(querySnapshot.documents[0]['firstName']);
if (querySnapshot == 'null') {
} else {
return '${querySnapshot.documents[0]['firstName']} ${querySnapshot.documents[0]['lastName']}';
}
// querySnapshot.documents.where((element) {
// print(element.documentID == 'firstName');
// });
});
}
I made simple singleton class to use on flutter StreamBuilder widget, for example:
StreamBuilder<User>(
stream: MydbModel.create().then((dao) => dao.getUserStream()) as Stream<User>,
builder: (_, snapshot) {
...
},
)
in that
MydbModel.create().then((dao) => dao.getUserStream())
return Future<Stream<User>> and my cast don't work to have only Stream<User>.
Error:
> type 'Future<Stream<User>>' is not a subtype of type 'Stream<User>' in type cast
how can i resolve this problem?
my completed class:
class MydbModel{
UserDao _userDao;
MydbModel._(this._userDao);
static Future<MydbModel> create() async => MydbModel._(await initialDatabase());
static Future<UserDao> initialDatabase() async {
var db = await $FloorAppDatabase.databaseBuilder('flutter_database.db').build();
return db.userDao;
}
Stream<User> getUserStream(){
return userDao.getUserInfo();
}
UserDao get userDao=>_userDao;
}
You need to await the Future first:
FutureBuilder(
future: MydbModel.create(),
builder: (BuildContext context, AsyncSnapshot<MydbModel> snapshot) {
if (!snapshot.hasData) return Container();
return StreamBuilder(
stream: snapshot.data.getUserStream(),
builder: ..,
);
},
);
I used a FutureBuilder to await the Future and to create a StreamBuilder when the future is loaded.