'FlutterFire Firebase Realtime Database showing 'Client is offline' error on Android, however, works fine on iOS
When running a simple query on a Firebase Realtime Database it works perfectly fine on iOS both in simulator and on device. However on Android it doesn't work. The code below attempts to get the user 'YYYY', whether or not the user already exists in the database. The security rules are not the issue, as this working on iOS. The code is as follows:
DatabaseReference _databaseRef = FirebaseDatabase(databaseURL:'https://XXXX.europe-west1.firebasedatabase.app/').reference();
DatabaseReference _userRef = _databaseRef.child('users/YYYY');
DataSnapshot userData = await _userRef.once();
DataSnapshot userData = await _userRef.get();
For the call await _userRef.once(), nothing happens and the function invocation stalls and never moves forward. The emulator will just continue to provide a stream of app_time_stats updates:
D/EGL_emulation: app_time_stats: avg=67.81ms min=7.44ms max=980.53ms count=19
D/EGL_emulation: app_time_stats: avg=16.53ms min=9.48ms max=19.88ms count=61
D/EGL_emulation: app_time_stats: avg=16.64ms min=14.06ms max=19.05ms count=61
D/EGL_emulation: app_time_stats: avg=11.45ms min=2.64ms max=65.59ms count=58
For the call await _userRef.get(), I get back the following error message on both Android emulator and physical device is:
I/RepoOperation: get for query /users/YYYY falling back to disk cache after error: Client is offline
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(get-failed, Client is offline, null, null)
If I try to interact with the database by creating a new user, no error message is received but no data appears in the realtime database. If I try to retrieve the user again, it thinks that the user creation was successful and returns me data. I suppose this is because it thinks the database is offline and it then just creates the data offline.
Also, all other Firebase related frameworks work fine for both iOS and Android. This includes e.g., Cloud Firestore.
Finally, after a while, the strangest thing is that this SOMETIMES suddenly starts working fine on Android, just as it does on iOS. However, I am not doing anything in particular that I can identify as the reason for it suddenly working.
Solution 1:[1]
Please make sure you have updated google-services.json / GoogleService-Info.plist . They have information about your DB after you enabled it, e.g.:
Android:
"firebase_url": "https://mydatabase.europe-west1.firebasedatabase.app",
iOS:
<key>DATABASE_URL</key>
<string>https://mydatabase.europe-west1.firebasedatabase.app</string>
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 | Oleg Novosad |
