Skip to content

🐛 [cloud_firestore] does not throw exception when the phone is without connection or internet #7636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
insinfo opened this issue Dec 16, 2021 · 5 comments
Labels
plugin: cloud_firestore resolution: invalid This doesn't seem right and might not belong here. type: bug Something isn't working

Comments

@insinfo
Copy link

insinfo commented Dec 16, 2021

cloud_firestore does not throw exception when the phone is without connection or internet, it puts everything in the log but it does not propagate the exception, it should throw an exception to be treated with a message to give feedback to the user.

class FirestoreService {
  static FirestoreService _instance = new FirestoreService.internal();
  FirestoreService.internal();
  factory FirestoreService() => _instance;

  FirebaseFirestore db = FirebaseFirestore.instance;

  Future<RList<Atracao>> fetchAllAtracoes() async {
    try {
      var collection = db.collection("atracoes");
      var query = collection.orderBy("nome", descending: false);
      var docs = await query.get();
  
      var list = new RList<Atracao>();
      docs.docs.forEach((item) {
        var data = item.data();
        data['id'] = item.id;
        list.add(Atracao.fromJson(data));
      });
  
      return list;
    } catch (e) {
      print("fetchAllAtracoes $e");
      throw FetchDataException(AppStrings.statusMessageErrorRequest, 400);
    }
  }
}

pubspec.yaml

name: appsescverao
description: Aplicativo do Sesc Verão Rio das Ostras
version: 5.0.0+5
publish_to: none

environment:
  sdk: ">=2.10.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  #cupertino_icons: ^0.1.3
  http: ^0.13.4 #Null safety full
  path_provider: ^2.0.8 #Null safety  full
  sqflite: ^2.0.1 #Null safety
  connectivity: ^3.0.6  #Null safety sera descontinuado final 2021 usar este connectivity_plus 2.0.2 no lugar
  url_launcher: ^6.0.17 #Null safety full

  firebase_core: ^1.10.5 #Null safety
  cloud_firestore: ^3.1.4 #Null safety
  firebase_auth: ^3.3.3 #Null safety
  #google_sign_in: ^5.2.1 #Null safety 
  #firebase_analytics: ^9.0.3 #Null safety 
  firebase_messaging: ^11.2.3 #Null safety

  rxdart: ^0.27.3 #Null safety full
  webview_flutter: ^3.0.0 #Null safety 
  auto_size_text: ^3.0.0 #Null safety full
  flutter_map: ^0.14.0 #Null safety full
  shared_preferences: ^2.0.10 #Null safety full
  latlong2: ^0.8.1 #Null safety full     
  share: ^2.0.4 #Null safety
  map_launcher: ^2.1.1  #Null safety
  #sticky_grouped_list: ^2.0.0-nullsafety.2  #Null safety
  scrollable_positioned_list: ^0.2.0-nullsafety.0
  grouped_list: ^4.1.0

  flutter_advanced_networkimage_2:
    path: ../../flutter_advanced_networkimage

  turismo_core:
    path: ../../portal_turismo/turismo_core
  

dev_dependencies:
  flutter_test:
    sdk: flutter

  #intl_translation: ^0.17.7
  flutter_native_splash: ^1.3.2
  #run flutter pub run flutter_native_splash:create
  
flutter:

  uses-material-design: true

  assets:
    - assets/
   

  fonts:
    - family: Big Noodle Titling
      fonts:
        - asset: fonts/big_noodle_titling.ttf
    - family: GlosaDisplayW01-Bold
      fonts:
        - asset: fonts/glosa_display_w01_bold.ttf
    - family: BakerieSmoothCondensedMedium
      fonts:
        - asset: fonts/bakerie_smooth_condensed_medium.otf
    - family: BakerieSmoothBold
      fonts:
        - asset: fonts/bakerie_smooth_bold.otf
    - family: iconsmind
      fonts:
        - asset: fonts/iconsmind.ttf

log

PS C:\MyDartProjects\appsescverao\appsescverao> flutter run
Multiple devices found:
ASUS X018D (mobile)          • JBAXB600T149ZSD • android-arm64 • Android 8.1.0 (API 27)
sdk gphone64 x86 64 (mobile) • emulator-5554   • android-x64   • Android 12 (API 31) (emulator)
[1]: ASUS X018D (JBAXB600T149ZSD)
[2]: sdk gphone64 x86 64 (emulator-5554)
Please choose one (To quit, press "q/Q"): 1
Launching lib\main.dart on ASUS X018D in debug mode...
Running Gradle task 'assembleDebug'...                             29,9s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...                16,8s
I/flutter (10303): initHomePageState:
Syncing files to device ASUS X018D...                              950ms

Flutter run key commands.
r Hot reload. 
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety

An Observatory debugger and profiler on ASUS X018D is available at: http://127.0.0.1:65071/gLOMsT81hpM=/
The Flutter DevTools debugger and profiler on ASUS X018D is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:65071/gLOMsT81hpM=/
W/DynamiteModule(10303): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(10303): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(10303): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/zygote64(10303): The ClassLoaderContext is a special shared library.
I/zygote64(10303): The ClassLoaderContext is a special shared library.
V/NativeCrypto(10303): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 287 native methods...
D/NetworkSecurityConfig(10303): No Network Security Config specified, using platform default
I/ProviderInstaller(10303): Installed default security provider GmsCore_OpenSSL
W/ManagedChannelImpl(10303): [{0}] Failed to resolve name. status={1}
W/Firestore(10303): (24.0.0) [WatchStream]: (7e2484d) Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:223)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.doResolve(DnsNameResolver.java:282)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:318)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/Firestore(10303):     at java.lang.Thread.run(Thread.java:764)
W/Firestore(10303): Caused by: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:141)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/Firestore(10303):     at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$JdkAddressResolver.resolveAddress(DnsNameResolver.java:631)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:219)
W/Firestore(10303):     ... 5 more
W/Firestore(10303): Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/Firestore(10303):     at libcore.io.Linux.android_getaddrinfo(Native Method)
W/Firestore(10303):     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:59)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:122)
W/Firestore(10303):     ... 9 more
W/Firestore(10303): }.
W/Firestore(10303): (24.0.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:223)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.doResolve(DnsNameResolver.java:282)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:318)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/Firestore(10303):     at java.lang.Thread.run(Thread.java:764)
W/Firestore(10303): Caused by: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:141)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/Firestore(10303):     at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$JdkAddressResolver.resolveAddress(DnsNameResolver.java:631)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:219)
W/Firestore(10303):     ... 5 more
W/Firestore(10303): Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/Firestore(10303):     at libcore.io.Linux.android_getaddrinfo(Native Method)
W/Firestore(10303):     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:59)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:122)
W/Firestore(10303):     ... 9 more
W/Firestore(10303): }
W/Firestore(10303): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
W/Firestore(10303): (24.0.0) [WatchStream]: (7e2484d) Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:223)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.doResolve(DnsNameResolver.java:282)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:318)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/Firestore(10303):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/Firestore(10303):     at java.lang.Thread.run(Thread.java:764)
W/Firestore(10303): Caused by: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:141)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/Firestore(10303):     at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver$JdkAddressResolver.resolveAddress(DnsNameResolver.java:631)
W/Firestore(10303):     at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:219)
W/Firestore(10303):     ... 5 more
W/Firestore(10303): Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/Firestore(10303):     at libcore.io.Linux.android_getaddrinfo(Native Method)
W/Firestore(10303):     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:59)
W/Firestore(10303):     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:122)
W/Firestore(10303):     ... 9 more
W/Firestore(10303): }.
I/flutter (10303): obteve da API
W/ManagedChannelImpl(10303): [{0}] Failed to resolve name. status={1}
W/ManagedChannelImpl(10303): [{0}] Failed to resolve name. status={1}
W/ManagedChannelImpl(10303): [{0}] Failed to resolve name. status={1}
@insinfo insinfo added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Dec 16, 2021
@darshankawar darshankawar added the triage Issue is currently being triaged. label Dec 17, 2021
@darshankawar
Copy link

@insinfo
Check this SO answer by former firebase member and see if it answers your question.

@darshankawar darshankawar added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Dec 17, 2021
@GMChristo
Copy link

@darshankawar, the answer on this link of stackoverflow has some problems. In one of them, when it did "if (snapshot.empty && snapshot.metadata.fromCache)", the problem is if the list is really empty on the base. Another problem is checking the connection before every access, if the internet is online when I check and in the next moment (access to the firebase) the internet fall, in this case, we return to the original problem.

@insinfo
Copy link
Author

insinfo commented Dec 17, 2021

@darshankawar the ideal is to have a global option to say whether to throw an exception or not, so that it can be handled by the API user and give feedback to the user of app, so that the user doesn't see just a void list

@GMChristo I totally agree, checking if you have internet every time you run an operation on the database is an overload, in addition to causing greater latency in displaying the data.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Dec 17, 2021
@darshankawar
Copy link

darshankawar commented Dec 20, 2021

Thanks for your input. Leaving this issue open and labeling it for further insights from the team.

Related SO link I shared earlier: https://stackoverflow.com/questions/63414747/how-to-catch-client-is-offline-error-in-flutter-firebase-firestore/63415042#63415042

Similar / related issue: #6749

@darshankawar darshankawar added plugin: cloud_firestore and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Dec 20, 2021
@russellwheatley
Copy link
Member

Hey folks, Firestore & Database do not throw errors when the users phone is offline by design. They are awaiting (in the hope) that they do come back online and sync up what is local with the remote database. If you would like to change this behaviour, you ought to raise it in the android or iOS SDK repo, but I can't imagine they will change something that is fundamental to Firestore's function. FlutterFire is just a wrapper around both iOS and android SDKs.

@darshankawar darshankawar added the resolution: invalid This doesn't seem right and might not belong here. label Jul 20, 2022
@firebase firebase locked and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: cloud_firestore resolution: invalid This doesn't seem right and might not belong here. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants