In the initState() of my StatefulWidget, I am calling a function called initializeNewGrid():
void initState() {
// get abbacus provider
abbacusProvider = Provider.of<AbbacusProvider>(context, listen: false);
// initialize new abbacus
abbacusProvider.initializeNewGrid(reset: false);
}
This function calls some more functions for initialization of the needed variables, one of these functions which has a return type of int is performing a lot of computations:
int generateProblem({required bool next}) {
//
// Code to generate a new problem
//
// return solution of the generated problem
return computeCorrectSolution();
}
such that when build() method is called the computation is not yet completed, and some of the lists that are not yet filled with data are tried to be accessed with the ListView.builder, which results in the following error:
The following RangeError was thrown building: I/flutter (23729): RangeError (index): Invalid value: Valid value range is empty: 0
I have tried using Future but haven't been able to do it properly. The build always executes before the complete initialization.
Try to change your initState as mentioned below
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
// get abbacus provider
abbacusProvider = Provider.of<AbbacusProvider>(context, listen: false);
// initialize new abbacus
abbacusProvider.initializeNewGrid(reset: false);
});
}