1

I have been working with Firestore and am successful in implementing it. However, when I test it offline, aiming to implement a respective UI display that there is no access to the server at that time or anything, I keep getting a the client is offline error and for some reason, there's no way for me to catch it.

Here's the error message:

Exception has occurred.
PlatformException (PlatformException(Error performing get, Failed to get document because the client is offline., null))

Here's also my sample code:

DocumentReference ref = firestoreDb.collection('collection').document('doc1');

try{
    ref.get().then((DocumentSnapshot ds){
        if(ds.exists == true){
            print('ds exists');
        }else{
            print('ds does not exist);
        }
    }).catchError((err){
        print(err);
    });
} on PlatformException catch (err){
    print(err);
} catch (err){
    print(err);
}

Any of the error catches in the sample code we're bypassed and the error is not actually caught. I need to atleast catch the client is offline exception so I can properly update my UI.

AverageCoder
  • 321
  • 6
  • 15

1 Answers1

1

With all of the Firestore client SDKs (including Flutter), errors are never thrown when a client is offline. If you're seeing an error in the log, that doesn't mean an exception is escaping from an API call. Tt just means that the SDK has chosen to log something internally. On other platforms, the SDK will also internally retry queries until the client comes back online, so I would assume the same to be true for Flutter.

Unfortunately, Firestore also does not provide a way for you to detect if you're offline.

See also: how to catch error in firestore when no internet

If you think the Flutter SDK is doing the wrong thing, I would encourage you to file a bug on GitHub.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for the quick response, it helps that I can look for another way sooner than staying on it longer. During my tests, it blocks my code on debug mode and the message is simply written in the terminal during release mode. I guess I just have to test connectivity before even reaching to that point then. Thanks also for the links, sorry I wasn't able to get to them on my initial research sooner. – AverageCoder Aug 14 '20 at 15:02
  • I don't know if it's just me but I hope there'll be something soon that we can use to directly address the offline status of a client from the Firestore object itself - maybe some convenience function. But for now, there'll always be another way. – AverageCoder Aug 14 '20 at 15:05
  • 1
    You can always file a feature request, but I don't think anything is going to change. It's been a request for years now. The documentation suggests other ways of managing presence. https://firebase.google.com/docs/firestore/solutions/presence – Doug Stevenson Aug 14 '20 at 15:08