在initState函数中的For循环中使用await不起作用。

huangapple go评论70阅读模式
英文:

Await inside For Loop not working in initState function

问题

I am trying to insert multiple records in initState() into the database using for loop as below:

void _insertManyAttendees() async {
    for (var i = 0; i < numberOfPeople; i++) {
      Map<String, dynamic> row = {'name': 'Attendee $i', 'billId': billId};
      await LocalStorageService.instance.insert(row, 'attendees');
    }
  }

However, the function above only inserts one record into the database when it is supposed to insert multiple records.

My initState() function:

@override
  void initState() {
    super.initState();
    // 1. Get the number of attendees
    numberOfPeople =
        Provider.of<BillData>(context, listen: false).numberOfPeople;

    // 2. ID of the new record in the database
    billId = Provider.of<BillData>(context, listen: false).id;
    debugPrint('inserted Row ID in input_attendees_screen: $billId');

    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 3. Create new records in the ATTENDEES table based on the number of people
      _insertManyAttendees();

      // 4. Find all ATTENDEES based on billId
      _findManyAttendees(billId);

      _findAllAttendees();
    });
  }

_findManyAttendees(billId) always returns only 1 record during the initial insertion, but in the next run, it always returns the correct number of records.

Wondering what went wrong. Many thanks in advance for the help.

英文:

I am trying to insert multiple records in initState() into the database using for loop as below:

void _insertManyAttendees() async {
    for (var i = 0; i < numberOfPeople; i++) {
      Map<String, dynamic> row = {'name': 'Attendee $i', 'billId': billId};
      await LocalStorageService.instance.insert(row, 'attendees');
    }
  }

However the function above only inserts one record to the database when it actually supposed to insert multiple records.

my initState() function:

@override
  void initState() {
    super.initState();
    // 1. Get number of attendees
    numberOfPeople =
        Provider.of<BillData>(context, listen: false).numberOfPeople;

    // 2. ID of new record in database
    billId = Provider.of<BillData>(context, listen: false).id;
    debugPrint('inserted Row ID in input_attendees_screen: $billId');

    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 3. Create new records in ATTENDEES table based on number of people
      _insertManyAttendees();

      // 4. Find all ATTENDEES based on billId
      _findManyAttendees(billId);

      _findAllAttendees();
    });
  }

_findManyAttendees(billId) always return only 1 record during the initial insertion but in the next run, it always return the correct number of records.

Wondering what went wrong. Many thanks in advance for the help.

答案1

得分: 1

以下是您要翻译的内容:

包括 Future<void>

Future<void> _insertManyAttendees() async {
  for (var i = 0; i < numberOfPeople; i++) {
    Map<String, dynamic> row = {'name': 'Attendee $i', 'billId': billId};
    await LocalStorageService.instance.insert(row, 'attendees');
  }
}

使用 await 调用该方法:

await _insertManyAttendees();
英文:

Include Future&lt;void&gt;:

Future&lt;void&gt; _insertManyAttendees() async {
  for (var i = 0; i &lt; numberOfPeople; i++) {
    Map&lt;String, dynamic&gt; row = {&#39;name&#39;: &#39;Attendee $i&#39;, &#39;billId&#39;: billId};
    await LocalStorageService.instance.insert(row, &#39;attendees&#39;);
  }
}

Call the method using await:

await _insertManyAttendees();

huangapple
  • 本文由 发表于 2023年4月17日 22:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定