【问题标题】:Flutter does not wait for database operationFlutter 不等待数据库操作
【发布时间】:2021-04-01 11:29:38
【问题描述】:

我正在学习 Flutter,我非常绝望。我正在为我的母亲编写一个小应用程序,该应用程序将在一天内用 Java 开发。在 Flutter 中,我正在寻找三天的时间来解决一些问题。我解决了一些问题,但现在我不知道该怎么办。目前,我不喜欢这种语言。

我的应用程序想要查询数据库。我有几个方法调用,下面的代码是真实代码的缩短版本。

  Future<List<GarbageCan>> startUseCase(String icsFilePath, int locationId) async {
    File icsFile = File(icsFilePath);

    List<String> lines = await icsFile.readAsLines();
    List<GarbageCan> garbageCans = List();
    String date;

    List<Appointment> appointments = List();

    lines.forEach((line) async {
      // Some string operations create the variable month, day, summary and so on. 

        Appointment appointment = Appointment(year: year,
            month: month,
            day: day,
            locationId: locationId,
            garbageCanName: summary);

        appointments.add(appointment);
      }
    });

    appointments.forEach((element) async {
      print(element);
      GarbageCan garbageCan = await _daoManager.findGarbageCanByName(element.garbageCanName);
      print("Tonnen\n");
      print(garbageCan);
      // Here I want to create the objects for the list that should be returned. 
    });

    print("***********************");
    // returns the list that is created in the for-each-statement above. 
    return garbageCans;
  }

我希望应用程序等到 findGarbageCanByName() 方法中的数据库查询完成。然后应该执行下一个语句。但是应用程序执行 findGarbageCanByName(),然后执行 return 语句,然后才执行 print(garbageCan);

但我需要应用程序首先执行 print(garbageCan);对于列表中的每个约会,然后执行 return 语句,因为我想在 foreach 语句中创建要返回的对象。

这是在另一个类中调用的方法:

Future<GarbageCan> findGarbageCanByName(String garbageCanName) async {
    return _garbageCanDao.findGarbageCanByName(garbageCanName);
  }

这个方法调用那个方法:

  Future<GarbageCan> findGarbageCanByName(String garbageCanName) async {
    final db = await database;
    String whereString = DatabaseConstants.WHERE_GARBAGE_CAN_NAME;
    List<dynamic> whereArguments = [garbageCanName];

    List<Map> result = await db.query(
        DatabaseConstants.GARBAGE_CAN_TABLE_NAME,
        where: whereString,
        whereArgs: whereArguments);
    return GarbageCan.fromMap(result.first);
  }

我正在阅读有关 Future、async 和 await 几个小时的内容,我以为我理解了这个概念。但现在,我不确定这一点。我认为 await 会阻塞操作,直到数据库查询完成。

有人知道该怎么做吗?

提前谢谢你。

克里斯托弗

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试替换此部分

    appointments.forEach((element) async {
      print(element);
      GarbageCan garbageCan = await _daoManager.findGarbageCanByName(element.garbageCanName);
      print("Tonnen\n");
      print(garbageCan);
      // Here I want to create the objects for the list that should be returned. 
    });
    

    for (final element in appointments) {
      print(element);
      GarbageCan garbageCan = await _daoManager.findGarbageCanByName(element.garbageCanName);
      print("Tonnen\n");
      print(garbageCan);
      // Here I want to create the objects for the list that should be returned. 
    }
    

    【讨论】:

    • 非常感谢。这实际上解决了我的问题。
    【解决方案2】:

    您可以在 statefulwidget 的 initState 中执行这些操作。 The framework will call this method exactly once for each State object it creates.

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多