When I try to reorder things in my reorderable list view, I run into a problem. If the user is online, awaiting the document references (as seen in the code) runs fast enough. The problem is, if the user is offline, the Firebase await function can take upwards of 20 seconds. On the other hand, if I don't await these changes, and do multiple quick reorders, the resulting writes as soon as I go back online differ greatly from the "order" state that I have in the app.
Is there anything I can do that avoids awaiting?
I have thought about checking the online status and disabling this feature while offline or disabling the reorder function while the function awaits. Both seem like really bad solutions.
Thanks for the help, the code is below:
Future<void> reorderExercises(int oldIndex, int newIndex,
CollectionReference<AddExercise> whereToUpdateRef) async {
List<DocumentReference<AddExercise>> referenceList = [];
await whereToUpdateRef.orderBy(COLUMN_DAYSINDEX).get().then(
(value) => value.docs.forEach(
(element) {
referenceList.add(element.reference);
},
),
);
void _updateIndex(int _oldIndex, int _newIndex) {
referenceList[_oldIndex].update(
{COLUMN_DAYSINDEX: _newIndex},
);
}
if (oldIndex < newIndex) {
newIndex -= 1;
_updateIndex(oldIndex, newIndex);
for (int i = oldIndex + 1; i <= newIndex; i++) {
_updateIndex(i, i - 1);
}
} else {
for (int i = oldIndex - 1; i >= newIndex; i--) {
_updateIndex(i, i + 1);
}
_updateIndex(oldIndex, newIndex);
}
}