AverageCoder
AverageCoder

Reputation: 341

How to catch Client is Offline error in Flutter Firebase Firestore

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.

Upvotes: 1

Views: 2882

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

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.

Upvotes: 1

Related Questions