我正在尝试在启动时检查互联网连接,并决定是否要打开包含来自 Firebase 的数据的页面或包含来自本地存储的数据的不同页面。 但它失败了(有时它工作一次,然后又卡在白屏上),错误消息如下所示)。 似乎只有在有互联网连接时才会出现问题(但我不确定)。
但是,如果我删除 try-catch 部分(连接检查),代码将按预期工作 - 没有错误(有或没有互联网)。
我正在 Android 模拟器 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'),
...
}
调试控制台中的错误消息:
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-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
我也尝试过使用connectivity_plus包,但结果相同。
我还将此添加到 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
它也没有帮助。
这不是测试互联网连接的好策略。 该设备可以缓存先前 DNS 查找的结果并返回结果,根本不需要任何互联网连接。 如果您想知道是否有互联网,您实际上应该尝试连接到设备无法缓存的某些服务。 您似乎已经从here复制了代码,但是您会在注释中注意到其他人也无法使其工作。