I have this piece of code:
List<String> allSlokamIdsList = [];
List<Slokam> allSlokamsList = [];
Map<String, List<String>> allSlokamsMap = {};
List<String> sungSlokams = [];
var knownSlokams = {'nma': <String>[], 'mpd': <String>[]};
late String currentAksharam;
late String currentVrtham;
var currentPlayerNo = 0;
late String currentSlokam;
@override
void initState() {
// ignore: todo
// TODO: implement initState
super.initState();
print('in sadas');
getData();
doSetup();
}
@override
void dispose() {
player.dispose();
super.dispose();
}
Future<void> getData() async {
allSlokamsList = await dbHelper.getSlokams();
allSlokamIdsList = await dbHelper.getSlokamIds();
// print(allSlokamsList[1].first_line);
}
void doSetup() async {
_init();
_getAudioList();
player.playbackEventStream.listen((event) {
if (event.processingState == ProcessingState.completed) {
slokamCompleted();
}
});
}
void _init() {
print('_init');
isPlaying = false;
isPaused = false;
isStopped = true;
print('allSlokamsList = $allSlokamsList');
for (var item in allSlokamsList) {
allSlokamsMap[item.slokam_id] = [
item.aksharam,
item.aksharam3,
item.slokam_text
];
}
print('allSlokamsMap=$allSlokamsMap');
}
The await in the getdata() is not waiting for the operation to finish, apparently.
When the code in _init() is executed, the getdata() is not complete and as a result, the allSlokamsList is emplty.
The print statements are producing the following output:
I/flutter (17648): in sadas
I/flutter (17648): _init
I/flutter (17648): allSlokamsList = []
I/flutter (17648): allSlokamsMap={}
How can I fix this?