Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

Flutter - stuck on multiple google sheets calls

New to flutter and need help. I'm using google sheets as a database. Supposed to use multiple sheets each with different sheetId.

I have 2 function to get the data:
getChildSchedule - to get data from one specific sheet
getAllSchedule - to get data from multiple sheets.

In the widget I'm checking if I'm supposed to display data from a particular sheet or from all the sheets and call the appropriate func. When I'm calling getChildSchedule from the widget all works perfectly and it shows the data.
But when I need the getAllSchedule it gets stuck. It doesn't stop running but seems as if it's in an infinite loop though there is no such loop to get stuck on. From the prints and the tracking I did, it calls on the getChild with index 0 but never returns from it - though the child data is being printed inside getChild.
What am I doing wrong here?

Future<List<Lesson>> getChildSchedule(int childId) async {
    print('in getChild: child $childId: ${ChildrenManager.children[childId].spreadsheetId}');
    spreadsheetId = ChildrenManager.children[childId].spreadsheetId;
    await init();
    final lessons = await _scheduleSheet.values.allRows(fromRow: 2);
    print('in getChild: child $childId lessons: $lessons');
    return List.generate(
      lessons.length,
      (index) => Lesson(
        weekDay: lessons[index][0],
        startTime: double.tryParse(lessons[index][1] ?? ''),
        endTime: double.tryParse(lessons[index][2] ?? ''),
        grade: ChildrenManager.children[childId].grade,
        teacher: lessons[index][3],
        header: lessons[index][4],
        description: lessons[index][5],
        zoomLink: Uri.tryParse(lessons[index][6] ?? ''),
        meetingID: lessons[index][7],
        meetingCode: lessons[index][8],
        supplies: lessons[index][9],
        assignment: Uri.tryParse(lessons[index][10] ?? ''),
      ),
    );
  }

  Future<List<Lesson>> getAllSchedule() async {
    List<List<Lesson>> schedules = List<List<Lesson>>();
    for (int i = 0; i < ChildrenManager.children.length; i++) {
      print('in getAll schedules: $i');
      schedules[i] = await getChildSchedule(i);
      print('in getAll schedules: got $i child');    //this never gets printed
    }
    print('in getAll schedules: $schedules');
    List<Lesson> schedule = List<Lesson>();
    for (List<Lesson> sc in schedules) {
      schedule.addAll(sc);
    }
    schedule.sort((a, b) {
      int result = a.startTime.compareTo(b.startTime);
      if (result == 0) {
        return a.endTime.compareTo(b.endTime);
      }
      return result;
    });
    return schedule;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the issue here was because of repeated calls that changed the sheet id while the previous was still running. I've moved the getAll to another class and called it with a new manager object(that contains the getChild) for each child and it solved the issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...