'fl_chart linechart firestore data limiting and displaying according to date

I am having difficulties in displaying the data in a lineChart. the data is from the firestore. when the chart is displayed the dots are in the wrong order.

this is how it is displayed, and is wrong:

wrong chart data

and this how it should show:

correct chart

it should be according to the date, but my first post date is February 24, and others are in March. How to exclude the previous month data, here February from the chart?

 SideTitles get bottomTitles => SideTitles(
  showTitles: true,
  margin: 8,
  getTitles: (value) {
  return (value.toInt().toString() +
      "/" +
      DateTime.now().month.toString());
  },
  getTextStyles: (context, value) => const TextStyle(
  color: Colors.black45,
  fontWeight: FontWeight.normal,
  fontSize: 12,
  ),
  );

this is where I get the data. I limit to last three and order it by date, then it is correct. But, as you know, it is not desirable as I will definitely get the issue again, if user posts data at the end of the month, and the next post at the beginning of the next month:

 final CollectionReference fs = usersRef
  .doc(firebaseAuth.currentUser!.uid)
  .collection('posts');

 List<UserDataModel> filteredList = [];

 Stream<List<UserDataModel>> chartData() {
return fs.orderBy("datePublished", descending: false)
    .limitToLast(3).snapshots().map((querySnapshot) {
  for (var documentSnapshot in querySnapshot.docs) {
    try {
      Post posts =
      Post.fromJson(documentSnapshot.data() as Map<String, dynamic>);

      filteredList.add(UserDataModel(
        double.parse(posts.bmr),
        DateTime.parse(posts.datePublished.toDate().toString()),
      ));
    } catch (e) {
      if (kDebugMode) {
        print('Error in preparing chart data: ${e.toString()}');
      }
    }
  }
  return filteredList;
});
}

please, help! Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source