'Flutter Firebase offline issues with await

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);
    }
  }


Solution 1:[1]

Problem:

await is very slow when the user has no internet on


Solution:

You check if the user has internet. if not you push to a noInternet() screen with a try again button.

You can check if the user have internet with this code:

   Source _source = Source.serverAndCache;

    try {
            final result = await InternetAddress.lookup('example.com');
            if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
                 _source = Source.serverAndCache;

            }
          } on SocketException catch (_) {
                 _source = Source.cache;

          }
    
    firebaseDocumentReference.get(GetOptions(source: _source))...

This is very close to Liam's answer, but I cannot edit it unfortunately. Using his code I changed the source option and now offline await works as fast as online. Also changed "google.com" to "expample.com" for it to work in China as well.

Solution 2:[2]

Problem:

await is very slow when the user has no internet on


Solution:

You check if the user has internet. if not you push to a noInternet() screen with a try again button.

You can check if the user have internet with this code:

var haveInternet;

  ...

    try {
            final result = await InternetAddress.lookup('google.com');
            if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
              haveInternet = true;
            }
          } on SocketException catch (_) {
            haveInternet = false;
          }

Hope it helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jacoo
Solution 2 liam spiegel