TypeError:无法读取未定义的属性(读取“getApp”);

问题描述 投票:0回答:1

因此,我使用以下代码将我的应用程序作为

Chrome
实例正常运行。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Check if the app is running on the web
  if (kIsWeb) {
    print("Running on Web - Initializing Firebase...");
    try {
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      print("Firebase initialized successfully for Web.");
    } catch (e) {
      print("Error initializing Firebase for Web: $e");
    }
  } else {
    print("Not running on Web - Firebase functionality disabled.");
  }

  runApp(
    DevicePreview(
      enabled: !kReleaseMode, // Enabled only in development mode
      builder: (context) => const MyApp(), // Wrap your app
    ),
  );
}

然后,我决定添加

Firebase Test Suit
并按照文档修改我的代码:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (kIsWeb || Platform.isIOS || Platform.isAndroid) {
    print("Running on Web/iOS/Android - Initializing Firebase...");

    if (kDebugMode) {
      try {
        FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
        await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
        print(
            "Firebase initialized successfully - DEVELOPMENT -  for Web/iOS/Android.");
      } catch (e) {
        print(e);
      }
    } else {
      try {
        await Firebase.initializeApp(
          options: DefaultFirebaseOptions.currentPlatform,
        );
        print(
            "Firebase initialized successfully - PRODUCTION -  for Web/iOS/Android.");
      } catch (e) {
        print(e);
      }
    }
  } else {
    print("Not running on Web/iOS/Android - Firebase functionality disabled.");
  }

  runApp(
    DevicePreview(
      enabled: !kReleaseMode, // Enabled only in development mode
      builder: (context) => const MyApp(), // Wrap your app
    ),
  );
}

现在,在 IDE 中我没有收到任何错误,它应该运行良好,但是我收到这种错误,并且我不知道如何修复它:

Exception has occurred.
TypeError: Cannot read properties of undefined (reading 'getApp')
packages/firebase_core_web/src/interop/core.dart 59:28               app$
packages/firebase_core_web/src/firebase_core_web.dart 360:48         <fn>
packages/firebase_core_web/src/firebase_core_web.dart 384:21         guardNotInitialized
packages/firebase_core_web/src/firebase_core_web.dart 360:13         app
packages/firebase_core/src/firebase.dart 79:41                       app
packages/cloud_firestore/src/firestore.dart 33:21                    get instance
packages/secret_flutter_project/main.dart 62:36                      <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 610:19  <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 634:23  <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 532:3   _asyncStartSync
packages/secret_flutter_project/main.dart 54:6                       main$
web_entrypoint.dart 24:31                                            <fn>
lib/ui_web/ui_web/initialization.dart 41:15                          <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 610:19  <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 634:23  <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 581:31  <fn>
dart-sdk/lib/async/zone.dart 1676:54                                 runUnary
dart-sdk/lib/async/future_impl.dart 204:18                           handleValue
dart-sdk/lib/async/future_impl.dart 902:44                           handleValueCallback
dart-sdk/lib/async/future_impl.dart 931:13                           _propagateToListeners
dart-sdk/lib/async/future_impl.dart 707:5                            [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 777:7                            callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                     _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                      _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 186:7   <fn>

有人可以帮我解决这个问题吗?我怀疑它与这些库有关,但不确定

flutter firebase dart firebase-authentication flutter-dependencies
1个回答
0
投票

所以基本上问题之一是我只在生产模式下初始化

Firebase.initializeApp()
,而不是在调试模式下初始化。 我们必须将该函数移出 if 块,就在
WidgetsFlutterBinding.ensureInitialized();
之后。

但问题依然存在。 然后,我从 github 下载了我的项目到一个不同的、新的、干净的目录中 - 粘贴了我的代码 - 它工作正常吗?

所以基本上是两个文件夹,旧的有 git,新的干净的没有 git。两者都有相同的代码,完全相同,但旧的给了我同样的错误,而新的运行正常......

我没有找到原因,继续前进。

这是正确的代码,带有

Firebase Testing Suite
:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (kIsWeb || Platform.isIOS || Platform.isAndroid) {
    print("Running on Web/iOS/Android - Initializing Firebase...");
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );

    if (kDebugMode) {
      try {
        FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
        await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
        print(
            "Firebase initialized successfully - DEVELOPMENT -  for Web/iOS/Android.");
      } catch (e) {
        print(e);
      }
    } else {
      print(
          "Firebase initialized successfully - PRODUCTION -  for Web/iOS/Android.");
    }
  } else {
    print("Not running on Web/iOS/Android - Firebase functionality disabled.");
  }
© www.soinside.com 2019 - 2024. All rights reserved.