Here is my code in which i am showing some notes which i send to firebase, now when i am fetching them back they are not according to the serial in which i send them, i am getting random serial of notes.
class Show_notes extends StatefulWidget {
const Show_notes({Key? key}) : super(key: key);
@override
_Show_notesState createState() => _Show_notesState();
}
class _Show_notesState extends State<Show_notes> {
CollectionReference ref= FirebaseFirestore.instance
.collection("users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.collection('Notes');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Show_Notes'),
centerTitle: true,
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
Colors.grey,
Colors.blueAccent,
Colors.grey,
]
)
),
),
),
body:SingleChildScrollView(
physics: ClampingScrollPhysics(),
child: Column(
children: [
FutureBuilder<QuerySnapshot>(
future: ref.get() ,
builder: (context,snapshot){
if(snapshot.hasData){
return ListView.builder(
physics: ClampingScrollPhysics(),
padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
shrinkWrap: true,
itemCount: snapshot.data!.docs.length,
itemBuilder: (context,index){
Map data= snapshot.data!.docs[index].data() as Map;
DateTime mynote_time=data['Time'].toDate();
String formattedTime =
DateFormat.yMMMd().add_jm().format(mynote_time);
return Column(
children: [
Card(
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
child: Text(
"${data['title']}",
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 20,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
child: Text(
"${data['description']}",
style: GoogleFonts.lato(
fontSize: 14,
),
),
),
Container(
alignment: Alignment.bottomRight,
child: Text(
formattedTime
),
)
],
),
borderOnForeground: true,
shadowColor: Colors.blueAccent,
color: Colors.grey[100],
elevation: 10,
),
],
);
}
);
}
else return Padding(
padding: EdgeInsets.fromLTRB(150, 200, 0, 50),
child: SpinKitPumpingHeart(
color: Colors.blueAccent,
size: 100,
duration: Duration(seconds: 2),
),
);
}),
],
),
),
floatingActionButton: Padding(
padding: const EdgeInsets.all(8.0),
child: FloatingActionButton(
backgroundColor: Colors.blueAccent[100],
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Write_Note()));
},
child: Icon(
Icons.add,
size: 20,
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
);
}
}
Below code is for sending my notes to Firebase
void addnote() async{
CollectionReference ref= FirebaseFirestore.instance
.collection("users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.collection('Notes');
var data={
'title': titletext.text,
'description': destext.text,
'Time': DateTime.now(),
};
ref.add(data);
}
CodePudding user response:
If I understand correctly what you mean by "serial", I guess you want your Notes
to be retrieved sorted in the same order that you added them to the collection.
If that's the case, you need to query sorted by your Time
field. For example:
FutureBuilder<QuerySnapshot>(
future: ref.orderBy('Time', 'asc').get(),
...
I honestly don't know how a dart DateTime
serializes to Firestore, so I would suggest you also change your timestamp field to an actual firestore timestamp:
var data={
'title': titletext.text,
'description': destext.text,
'Time': FieldValue.serverTimestamp(),
};
ref.add(data);