简体   繁体   中英

Future function fetching Firebase data doesn't return the desired output

The function I am using to fetch my data

Future<List> readTable(List<Date> dates, List<Plot> plots) async {
    List rows = [];
    await userCollection.doc(uid).collection('plots').get().then(
          (QuerySnapshot snapshot) => {
            // ignore: avoid_function_literals_in_foreach_calls
            snapshot.docs.forEach(
              (plot) async {
                List row = [];
                for (Date d in dates) {
                  List works = [];
                  print(firebaseDocumentDate(d.dateTime));
                  await userCollection
                      .doc(uid)
                      .collection('plots')
                      .doc(plot['name'])
                      .collection(firebaseDocumentDate(d.dateTime))
                      .get()
                      .then(
                    (QuerySnapshot date) {
                      for (var work in date.docs) {
                        works.add(
                          Work(
                            work.id,
                            work['category'],
                            work['worker'],
                            work['description'],
                            work['status'],
                          ),
                        );
                      }
                    },
                  );
                  row.add(works);
                }
                rows.add(row);
                print(rows);
              },
            ),
          },
        );
    return rows;
  }

The "Date" class

class Date {
  final DateTime dateTime;
  final String text;

  Date(this.dateTime, this.text);
}

The "Work" class

class Work {
  final String id;
  final String category;
  final String worker;
  final String description;
  final String status;

  Work(
    this.id,
    this.category,
    this.worker,
    this.description,
    this.status,
  );
}

Firebase Cloud Firestore database

A diagramatic representation of my database

The output of the print statement.

flutter: [[[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [Instance of 'Work'], [], [], [Instance of 'Work'], [], []], [[], [], [], [], [], [], []]]

But when I print the final output it is an empty list. flutter: []

I have worked with a similar function on the same project.

  Future<List<Plot>> readPlots() async {
    List<Plot> plots = [];
    await userCollection.doc(uid).collection('plots').get().then(
          (QuerySnapshot snapshot) => {
            snapshot.docs.forEach(
              (plot) {
                plots.add(
                  Plot(plot["name"]),
                );
              },
            )
          },
        );
    return plots;
  }

This works well. I think the await function is so lengthy that flutter simply returns the empty list I declared in the function.

I was expecting the output that that the print statement inside the function returns. flutter: [[[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [Instance of 'Work'], [], [], [Instance of 'Work'], [], []], [[], [], [], [], [], [], []]]

Fixed it myself by taking in a list of plots from another function and running it over with a for loop instead of fetching it within the function.

Future<List> readTable(List<Date> dates, List<Plot> plots) async {
    List rows = [];
    for (Plot plot in plots) {
      List row = [];
      for (Date d in dates) {
        await userCollection
            .doc(uid)
            .collection('plots')
            .doc(plot.name)
            .collection(firebaseDocumentDate(d.dateTime))
            .get()
            .then(
          (QuerySnapshot date) {
            List works = [];
            for (var work in date.docs) {
              works.add(
                Work(
                  work.id,
                  work['category'],
                  work['worker'],
                  work['description'],
                  work['status'],
                ),
              );
            }
            row.add(works);
          },
        );
      }
      rows.add({'rowName': plot.name, 'cells': row});
    }
    return rows;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM