I receive the error The non-nullable local variable 'box' must be assigned before it can be used.
for the following code.
int problemClass = -1;
problems.forEach((p) async {
if (p.problemClassId != problemClass) {
if (problemClass != -1)
box.close();
box = await Hive.openBox(kProblemBoxNames[p.problemClassId]!);
problemClass = p.problemClassId;
}
}
I am opening a Hive box in the first iteration (box.close()
cannot be executed because problemClass == -1
). If problemClass
does not change, the box remains unchanged in the 2nd iteration. However, if problemClass
does change, the box currently open is closed and another box is opened.
How do I get the compiler to understand my logic?!