im failing to order my data by timestamp in descending order, error keeps saying have positioned it wrong
StreamBuilder(
stream: Firestore.instance.collection("payments").orderBy("timestamp", "desc").where('participants', arrayContains: userActive).snapshots(),
builder: (context, snapshot){
return Container ( child:ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
padding: EdgeInsets.all(0),
controller: ScrollController(keepScrollOffset: false),
itemBuilder: (context, index){
DocumentSnapshot documentSnapshot = snapshot.data.documents[index].data();
You should use the following:
orderBy("timestamp", descending : true)
https://github.com/FirebaseExtended/flutterfire/blob/f5a408f0aed529da5602d4562964d60ff50d2a7e/packages/cloud_firestore/cloud_firestore/lib/src/query.dart#L224
Related
I have a collection named 'room', inside which I've a document named 'users', inside the doc I've another collection named 'users' inside which I've filed named 'userName'. I want to access the 'userName'
Image 1, Image 2, Image 3
Heres what I've tried
StreamBuilder(
stream: FirebaseFirestore.instance.collection(widget.roomId).doc("users").collection("users").snapshots(),
builder: (context, newSnapshot){
final documents = newSnapshot.data?.docs;
if (newSnapshot.connectionState == ConnectionState.waiting){
return Text("WAITING");
}
return ListView.builder(
itemCount: documents?.length,
itemBuilder: (context, index) => Container(
height: 25,
child: Text(documents![index]["usersName"] ?? "nothing found"),
));
},
),
Seems like you have misspelled the userName key in your text widget
child: Text(documents![index['usersName']??'Nothing found');
should be
child: Text(documents![index]['userName']??'Nothing found');
I'm making a to-do list with Flutter and Firebase Firestore, but I have a slight problem. Whenever I add a new task, snapshot.data becomes null. If I do not add a check for snapshot.connectionState and render a different widget according to that, I get the following error: NoSuchMethodError (NoSuchMethodError: The getter 'docs' was called on null.
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('tasks')
.doc(widget.uid)
.collection('mytasks')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: SizedBox(
height: 100, width: 100, child: CircularProgressIndicator()),
);
} else {
> final docs = snapshot.data.docs;
return ListView.builder(
itemCount: docs.length,
itemBuilder: (ctx, index) {
final currTask = docs[index];
return Dismissible(
direction: DismissDirection.startToEnd,
key: UniqueKey(),
onDismissed: (_) async {
FirebaseFirestore.instance
.collection('tasks')
.doc(widget.uid)
.collection('mytasks')
.doc(currTask['id'])
.delete();
.
.
.
I don't want to have to display a CircularProgressIndicator or an empty screen. I want the tasks to remain visible and seamlessly add new tasks. How can I achieve that? I know what I asked for in the question title might be a little silly, what should I do instead?
I want to retreive documents from my message collection; but i don't get the result and i have no error from flutter. Here ise my code
body: new Center(
child: new StreamBuilder(
stream: Firestore.instance.collection("messages").snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData) return const Text("Chargement ...");
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index)=> new Text("papa"),
);
}
),
)
and here is my firebase database
Use new Text(document['papa'])
Check docs: https://pub.dartlang.org/packages/cloud_firestore
Currently my I have a ListView that is warped by a StreamBuilder which gets data from firebase firestore (e.g a list of users). This is how it looks:
Widget UsersList = new StreamBuilder(
stream: Firestore.instance
.collection('users')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text("loading");
return new ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildItem(context, snapshot.data.documents[index]),
);
}
);
The question is how to add to the top of the ListView a static widget (e.g. a button to create a new user), I don't want the button to stay on the top of the page all the time, it should scroll with the ListView.
A workaround: in the _buildItem() function I could receive a boolean if it is the first document (by passing to the function index==0), and if true build the static widget (e.g. the add user button) first. But I can think of three problems:
If there isn't any documents in the firestore collection, it won't render the static widget.
If the internet connection is slow, it won't render the static widget until the first document is downloaded.
It is a workaround...
You could check the length inside the ListView.builder and always add an item for the button.
Widget UsersList = new StreamBuilder(
stream: Firestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
return new ListView.builder(
itemCount: (snapshot?.data?.documents?.length ?? 0) + 1,
itemBuilder: (context, index) {
if (index == 0)
return FlatButton(child: Text("Add"));
else
_buildItem(context, snapshot.data.documents[index-1]);
},
);
},
),
My Firebase Structure is like this:
I want to get 'groupName' list in FirebaseAnimatedList.
How to achive this?
My current code:
chatGroupNameReference = FirebaseDatabase.instance.reference().child("chat_group_message_user_info").child(widget.user.uid);
body: new Container(
child: new Flexible(
child: new FirebaseAnimatedList(
query: chatGroupNameReference,
padding: const EdgeInsets.all(8.0),
defaultChild: Center(child: CircularProgressIndicator(backgroundColor: Colors.deepPurpleAccent,),),
itemBuilder: (_, DataSnapshot messageSnapshot,
Animation<double> animation, int index) {
return Text("Group Name will be here")
},
),
)
)
);
For nested child key query we need FutureBuilder with FirebaseAnimatedList:
chatGroupNameReference = FirebaseDatabase.instance.reference().child("chat_group_message_user_info").child(widget.user.uid);
body: new FirebaseAnimatedList(
defaultChild: Center(child: new CircularProgressIndicator()),
query: chatGroupNameReference,
sort: (a,b) => (b.key.compareTo(a.key)),
itemBuilder: (_, DataSnapshot messageSnapshot, Animation<double> animation, int index) {
return new FutureBuilder<DataSnapshot>(
future: chatGroupNameReference.child(messageSnapshot.key).once(),
builder: (BuildContext context, snapshot){
return snapshot.hasData ? new Text(snapshot.data.value['groupName']) : new Text("");
},
);
},
(_, DataSnapshot messageSnapshot,
Animation<double> animation, int index)
After that line you can use FutureBuilder to get data from your Firebase Database.
For example like that In a firebase animated list, is there a way to let firebase list know the expected height of a widget before it loads?