英文:
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<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');
}
}
Call the method using await:
await _insertManyAttendees();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论