skip to Main Content

I am trying to check internet connection on startup and decide whether I want to open page with data from Firebase or different page with data from local storage.
But it fails (sometimes it works one time and then it gets stuck on white screen again) with error messages shown below).
It seems the problem is only when there is internet connection (but I am not sure).

But if I remove the part try-catch (connectivity check), the code works as expected – no errors (with and without internet).

I am running the app on Android emulator Pixel 3A API 34.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  bool hasConnection = false;

  try {
    final result = await InternetAddress.lookup('example.com');
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      hasConnection = true;
    }
  } on SocketException catch (_) {
    hasConnection = false;
  }


  configureDependencies();

  runApp(MyApp(hasConnection: hasConnection,));
class MyApp extends StatelessWidget {
  final bool hasConnection;

  const MyApp({super.key, required this.hasConnection});

  @override
  Widget build(BuildContext context) {
    final providers = [EmailAuthProvider()];

    return MaterialApp(
      initialRoute: !hasConnection ? '/offlinePage' : (FirebaseAuth.instance.currentUser == null ? '/login' : '/home'),
...
}

Error messages in debug console:

Launching libmain.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
√  Built buildappoutputsflutter-apkapp-debug.apk.
Debug service listening on ws://127.0.0.1:55397/cIA527ooeTQ=/ws
Syncing files to device sdk gphone64 x86 64...
D/FirebaseAuth( 7833): Notifying id token listeners about user ( RsDL8sBMbZeMVyO1fFMTyVx4axV2 ).
W/DynamiteModule( 7833): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 7833): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 7833): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
D/nativeloader( 7833): Configuring clns-7 for other apk /system/framework/org.apache.http.legacy.jar. target_sdk_version=33, uses_libraries=ALL, library_path=/product/priv-app/PrebuiltGmsCore/lib/x86_64:/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86_64, permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms
D/nativeloader( 7833): Configuring clns-8 for other apk /system/framework/com.android.media.remotedisplay.jar. target_sdk_version=33, uses_libraries=ALL, library_path=/product/priv-app/PrebuiltGmsCore/lib/x86_64:/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86_64, permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms
D/nativeloader( 7833): Configuring clns-9 for other apk /system/framework/com.android.location.provider.jar. target_sdk_version=33, uses_libraries=ALL, library_path=/product/priv-app/PrebuiltGmsCore/lib/x86_64:/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86_64, permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms
D/ApplicationLoaders( 7833): Returning zygote-cached class loader: /system_ext/framework/androidx.window.extensions.jar
D/ApplicationLoaders( 7833): Returning zygote-cached class loader: /system_ext/framework/androidx.window.sidecar.jar
D/nativeloader( 7833): InitLlndkLibrariesProduct: libEGL.so:libGLESv1_CM.so:libGLESv2.so:libGLESv3.so:libRS.so:libandroid_net.so:libbinder_ndk.so:libc.so:libcgrouprc.so:libclang_rt.asan-x86_64-android.so:libcom.android.tethering.connectivity_native.so:libdl.so:libft2.so:liblog.so:libm.so:libmediandk.so:libnativewindow.so:libneuralnetworks.so:libselinux.so:libsync.so:libvndksupport.so:libvulkan.so
D/nativeloader( 7833): Configuring product-clns-10 for unbundled product apk /product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk. target_sdk_version=33, uses_libraries=, library_path=/product/priv-app/PrebuiltGmsCore/lib/x86_64:/product/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86_64:/product/lib64:/system/product/lib64, permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms:/product/lib64:/system/product/lib64
D/nativeloader( 7833): InitVndkspLibrariesProduct: android.hardware.common-V2-ndk.so:android.hardware.common.fmq-V1-ndk.so:android.hardware.graphics.allocator-V2-ndk.so:android.hardware.graphics.common-V4-ndk.so:[email protected]:[email protected]:[email protected]:android.hardware.graphics.composer3-V1-ndk.so:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:libRSCpuRef.so:libRSDriver.so:libRS_internal.so:libbase.so:libbcinfo.so:libblas.so:libc++.so:libcompiler_rt.so:libcutils.so:libdmabufheap.so:libgralloctypes.so:libhardware.so:libhidlbase.so:libhidlmemory.so:libion.so:libjsoncpp.so:liblzma.so:libprocessgroup.so:libunwindstack.so:libutils.so:libutilscallstack.so:libz.so
V/NativeCrypto( 7833): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 295 native methods...
W/my_app( 7833): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (unsupported, reflection, allowed)
I/ProviderInstaller( 7833): Installed default security provider GmsCore_OpenSSL
D/ProfileInstaller( 7833): Installing profile for com.jestrabikr.my_app
D/TrafficStats( 7833): tagSocket(133) with statsTag=0xffffffff, statsUid=-1
W/my_app( 7833): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (unsupported, reflection, allowed)
W/my_app( 7833): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (unsupported, reflection, allowed)
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
E/emuglGLESv2_enc( 7833): device/generic/goldfish-opengl/system/GLESv2_enc/GL2Encoder.cpp:s_glGetUniformLocation:2206 GL error 0x502
D/EGL_emulation( 7833): app_time_stats: avg=523.49ms min=14.68ms max=817.41ms count=3
D/EGL_emulation( 7833): app_time_stats: avg=410.16ms min=77.69ms max=1033.93ms count=3

I have also tried using connectivity_plus package, but with the same result.
Also I have added this into AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> and it did not help either.

2

Answers


  1. Chosen as BEST ANSWER

    It seems I have figured it out. The problem was with the android emulator (version 34.2.15-11906825). Solved it by changing renderer settings as instructed by this answer: https://github.com/flutter/flutter/issues/146890#issuecomment-2123136901


  2. This isn’t a good strategy for testing internet connection. The device could be caching the result of a prior DNS lookup and returning that result without needing any internet connection at all. You seem to have copied the code from here, but you’ll notice in the comments that others also can’t make it work.

    See: https://android.stackexchange.com/questions/220054/dns-queries-are-cached-permanently-how-to-reliably-clear-dns-cache-on-android-7

    One way to test if this is the case is to first restart your device or emulator and make sure it fully starts up without any internet enabled, then run this code. It might work the first time (given the cache is empty), but then if you enable internet and run it again, it might then work and cache the result indefinitely.

    If you want to know if you have internet, you should actually try to connect to some service that can’t be cached by the device or any SDK in use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search