Flutter从数据模型中获取Firestore数据并将其用作图表数据

时间:2020-09-21 21:20:18

标签: flutter dart charts google-cloud-firestore

我有一个看起来像这样的数据模型:

    class PieChartDataGraph {
      DateTime date;
      String books;
      PieChartDataGraph(this.date, this.books);
      PieChartDataGraph.fromJSON(Map<String, dynamic> json) {
        date = json['date'];
        books = json['books'];
      }
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = Map<String, dynamic>();
        data['date'] = this.date;
        data['books'] = this.books;
        return data;
      }
    }

然后我尝试从Firestore获取数据:

Future<List<PieChartDataGraph>>
                                                      getPieChartData() async {
                                                    FirebaseUser user =
                                                        await _auth
                                                            .currentUser();
                                                    var val = await Firestore
                                                        .instance
                                                        .collection("users")
                                                        .document(user.uid)
                                                        .collection("moments")
                                                        .orderBy('date')
                                                        .where('date',
                                                            isGreaterThanOrEqualTo: isWeekly
                                                                ? today.subtract(
                                                                    Duration(
                                                                        days: today
                                                                            .weekday))
                                                                : today.subtract(
                                                                    Duration(
                                                                        days:
                                                                            30)))
                                                        
                                                        .getDocuments();

                                                    var documents =
                                                        val.documents;

                                                    if (documents.length > 0) {
                                                      try {
                                                        return documents
                                                            .map((document) {
                                                          PieChartDataGraph
                                                              pieChartData =
                                                              PieChartDataGraph
                                                                  .fromJSON(Map<
                                                                          String,
                                                                          dynamic>.from(
                                                                      document
                                                                          .data));
                                                          return pieChartData;
                                                        });
                                                      } catch (e) {
                                                        return [];
                                                      }
                                                    }
                                                    return [];
                                                  }

如何使用它从Firestore数据库中获取数据,然后动态地将数据作为图表的数据点返回(在这种情况下,对于折线图和饼图)。

请注意,我正在查看FL Chart软件包。

谢谢您的帮助。

0 个答案:

没有答案