Is it a good / working practice to use Firebase's documentReference.get(GetOptions(source: cache)) in Flutter?

Viewed 46

My issue was, that with the default GetOptions (omitting the parameter), a request like the following could load seconds if not minutes if the client is offline:

    await docRef.get()...

If I check if the client is offline and in this case purposefully change the Source to Source.cache, I have performance that is at least as good, if not better, than if the client was online.

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

and then use this variable in the following way:

   docRef.get(GetOptions(source: _source))
   .then(...

This code works perfectly for me now, but I am not sure, if there are any cases in which using the code like this could raise issues.

Also it seems like a lot of boilerplate code (I refactored it into a function so I can use it in any Database methods but still...)

If there are no issues with this, why wouldn't this be the Firebase default, since after trying the server for an unpredictably long time it switches to cache anyways.

0 Answers
Related