为什么flutter的Future函数要运行几次?

问题描述 投票:0回答:1

我有这样的代码

  _CurrentDayPageState() {
    getSchedule().then((s) => setState(() {
      _todaySchedule = s;
    }));
  }

  ...

  Future<List<Map<String, dynamic>>> getSchedule() async {

    DateTime now = new DateTime.now();
    List<String> todayBorders = [DatabaseHelper.getStartOfTheDay(now), DatabaseHelper.getEndOfTheDay(now)];

    print(todayBorders);

    List<Map<String, dynamic>> schedule = await dbHelper.queryRowsSql("SELECT * FROM ${tableSchedule} WHERE "
        "Datetime(${columnDateTime}) BETWEEN Datetime('${todayBorders[0]}') AND Datetime('${todayBorders[1]}')");

    print("get schedule gone");

    return schedule;
  }

并有这样的输出。

I/flutter (15242): [2020-06-15 00:00:00, 2020-06-15 23:59:59]
I/flutter (15242): [2020-06-15 00:00:00, 2020-06-15 23:59:59]
I/flutter (15242): [2020-06-15 00:00:00, 2020-06-15 23:59:59]
I/flutter (15242): get schedule gone
I/flutter (15242): get schedule gone
I/flutter (15242): get schedule gone

如果我使用FutureBuilder,我有同样的三个函数调用。是我的错还是我忘了什么?谢谢你的反馈

已解决这是因为页面所在的bottomNavigationBar自己重建了很多次,因为它有childs。

flutter asynchronous dart future
1个回答
0
投票

这是因为可能在建立状态时调用了这个函数,你的widget树中的一些东西正在重建状态,因此再次调用这个函数。

© www.soinside.com 2019 - 2024. All rights reserved.