如何将 Flutter 项目迁移到 Firebase 版本 9 现代 Web 模块化风格

Posted

技术标签:

【中文标题】如何将 Flutter 项目迁移到 Firebase 版本 9 现代 Web 模块化风格【英文标题】:How to migrate Flutter project to Firebase version-9 modern web modular style 【发布时间】:2021-11-03 17:35:07 【问题描述】:

我在初始化 FirebaseFlutter 项目时遇到问题,而我刚刚将我的 firebase-toolsfirebase 更新到 版本 9 这是 现代网络模块化风格 并且已经一周没有任何帮助 Flutter 的文档仍然根据旧式兼容类型的版本 8 并且当我关注更新其抛出错误的 firebase 文档时颤振。我需要你的帮助 firebase 和颤振专家请提供一些解决方案。

我得到的两个错误是,

core.dart:56 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'app')
    at Object.app$ [as app] (core.dart:56)
    at initializeApp (firebase_core_web.dart:68)
    at initializeApp.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at firebase_core_web.FirebaseCoreWeb.new.initializeApp (firebase_core_web.dart:28)
    at initializeApp (firebase.dart:42)
    at initializeApp.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at Function.initializeApp (firebase.dart:38)
    at main$ (main.dart:9)
    at main$.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at main$ (main.dart:7)
    at main (web_entrypoint.dart:19)
    at main.next (<anonymous>)
    at async_patch.dart:45
    at _RootZone.runUnary (zone.dart:1613)
    at _FutureListener.thenAwait.handleValue (future_impl.dart:155)
    at handleValueCallback (future_impl.dart:707)
    at Function._propagateToListeners (future_impl.dart:736)
    at _Future.new.[_completeWithValue] (future_impl.dart:542)
    at future.dart:401
    at _RootZone.runUnary (zone.dart:1613)
    at _FutureListener.then.handleValue (future_impl.dart:155)
    at handleValueCallback (future_impl.dart:707)
    at Function._propagateToListeners (future_impl.dart:736)
    at _Future.new.[_completeWithValue] (future_impl.dart:542)
    at async._AsyncCallbackEntry.new.callback (future_impl.dart:580)
    at Object._microtaskLoop (schedule_microtask.dart:40)
    at _startMicrotaskLoop (schedule_microtask.dart:49)
    at async_patch.dart:166

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
js_primitives.dart:30 The following JSNoSuchMethodError was thrown building MyApp(dirty):
js_primitives.dart:30 TypeError: Cannot read properties of undefined (reading 'app')
js_primitives.dart:30 
js_primitives.dart:30 The relevant error-causing widget was:
js_primitives.dart:30   MyApp file:///C:/Users/Neha/androidStudioProjects/devcom/lib/main.dart:10:10
js_primitives.dart:30 
js_primitives.dart:30 When the exception was thrown, this was the stack:
js_primitives.dart:30 C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49  throw_
js_primitives.dart:30 packages/firebase_core_web/firebase_core_web.dart 73:3                                                                     _catchJSError
js_primitives.dart:30 packages/firebase_core_web/src/firebase_core_web.dart 138:13                                                               app
js_primitives.dart:30 packages/firebase_core/src/firebase.dart 55:41                                                                             app
js_primitives.dart:30 packages/firebase_auth/src/firebase_auth.dart 38:47                                                                        get instance
js_primitives.dart:30 packages/devcom/auth.dart 12:51                                                                                            new
js_primitives.dart:30 packages/devcom/main.dart 17:13                                                                                            build
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 4648:28                                                                        build
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 4574:15                                                                        performRebuild
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 4267:5                                                                         rebuild
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 4553:5                                                                         [_firstBuild]
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 4548:5                                                                         mount
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 3611:13                                                                        inflateWidget
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 3363:18                                                                        updateChild
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 1189:16                                                                          [_rebuild]
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 1159:5                                                                           mount
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 1104:16                                                                          <fn>
js_primitives.dart:30 packages/flutter/src/widgets/framework.dart 2535:19                                                                        buildScope
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 1103:12                                                                          attachToRenderTree
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 936:24                                                                           attachRootWidget
js_primitives.dart:30 packages/flutter/src/widgets/binding.dart 917:7                                                                            <fn>
js_primitives.dart:30 C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:19       internalCallback
js_primitives.dart:30 
js_primitives.dart:30 ════════════════════════════════════════════════════════════════════════════════════════════════════

main.dart

import 'package:flutter/material.dart';
import 'package:devcom/auth.dart';
import 'package:devcom/auth_provider.dart';
import 'package:devcom/root_page.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async 
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp();
  runApp(MyApp());


class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return AuthProvider(
      auth: Auth(),
      child: MaterialApp(
        title: 'Flutter login demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: RootPage(),
      ),
    );
  

index.html

<!DOCTYPE html>
  <html>
  <head>
    <!-- <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js" type = "module"></script>
    <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-analytics.js" type = "module"> </script>
    <script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-auth.js" type = "module"></script> -->
    <meta charset="UTF-8">
    <meta content="IE=Edge" http-equiv="X-UA-Compatible">
    <meta name="description" content="A new Flutter project.">
  
    <!-- ios meta tags & icons -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="devcom">
    <link rel="apple-touch-icon" href="icons/Icon-192.png">
  
    <!-- Favicon -->
    <link rel="shortcut icon" type="image/png" href="favicon.png" />
  
    <title>Devcom</title>
    <link rel="manifest" href="manifest.json">
  </head>
<body >
  <!-- The core Firebase JS SDK is always required and must be listed first -->
 
<script type="module"> 
  import  initializeApp  from 'https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js';
  import    from 'https://www.gstatic.com/firebasejs/9.0.1/firebase-auth.js';
  import  getAnalytics  from "https://www.gstatic.com/firebasejs/9.0.1/firebase-analytics.js";


    const firebaseConfig = 
     apiKey: "...",
      authDomain: "......",
      databaseURL: ".....",
      projectId: "...",
      storageBucket: "",
      messagingSenderId: "",
      measurementId: "",
      appId: "1:.........:web:...........",
    ;
    
    const app = initializeApp( firebaseConfig );
// const app = getAnalytics( firebaseConfig )    
</script>
 
 <script>
    var serviceWorkerVersion = null;
    var scriptLoaded = false;
    function loadMainDartJs() 
      if (scriptLoaded) 
        return;
      
      scriptLoaded = true;
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    

    if ('serviceWorker' in navigator) 
      // Service workers are supported. Use them.
      window.addEventListener('load', function () 
        // Wait for registration to finish before dropping the <script> tag.
        // Otherwise, the browser will load the script multiple times,
        // potentially different versions.
        var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => 
            function waitForActivation(serviceWorker) 
              serviceWorker.addEventListener('statechange', () => 
                if (serviceWorker.state == 'activated') 
                  console.log('Installed new service worker.');
                  loadMainDartJs();
                
              );
            
            if (!reg.active && (reg.installing || reg.waiting)) 
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing ?? reg.waiting);
             else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) 
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log('New service worker available.');
              reg.update();
              waitForActivation(reg.installing);
             else 
              // Existing service worker is still good.
              console.log('Loading app from service worker.');
              loadMainDartJs();
            
          );

        // If service worker doesn't succeed in a reasonable amount of time,
        // fallback to plaint <script> tag.
        setTimeout(() => 
          if (!scriptLoaded) 
            console.warn(
              'Failed to load app from service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs();
          
        , 4000);
      );
     else 
      // Service workers not supported. Just drop the <script> tag.
      loadMainDartJs();
    
  </script>
 
</body>
</html>

【问题讨论】:

【参考方案1】:

Firebase 的 Flutter Web 绑定构建在 v9 之前的 API 之上。据我在 Github 存储库中看到的,还没有 discussion 开始将它们移植到 v9。我建议将它留给库的作者来决定何时迁移到 v9 模块化 API 样式的最佳时间,并在此之前使用 v8,如 adding the Firebase SDKs to your web app 上的 FlutterFire 文档中所示。

如果您坚持已经迁移到 v9,则可以使用其 -compat 构建,它将以前的 API 样式固定在新的模块化版本上。这将允许您使用最新版本,尽管它不会为您提供新模块化 API 的(完整)大小优势。

【讨论】:

我是 firebase 新手,那么如何将我的 firebase 版本降级到 flutter 支持的版本,目前我猜是 8.6.1 或使用 comat version-9 of firebase with flutter 是否使用 npm 卸载 Firebase 并再次安装以确保它与 Flutter 兼容,否则我们需要指定它 我链接的文档准确显示了要添加的内容,确实是 8.6.1 版,例如 &lt;script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"&gt;&lt;/script&gt;。完成此操作后,您还可以使用代码 sn-p 来初始化该页面上显示的 Firebase。【参考方案2】:

您的颤振代码不符合文档的建议,尤其是在哪里以及如何调用 Firebase.initializeApp();

这是直接来自docs的sn-p:

import 'package:flutter/material.dart';

// Import the firebase_core plugin
import 'package:firebase_core/firebase_core.dart';

void main() 
  WidgetsFlutterBinding.ensureInitialized();
  runApp(App());


/// We are using a StatefulWidget such that we only create the [Future] once,
/// no matter how many times our widget rebuild.
/// If we used a [StatelessWidget], in the event where [App] is rebuilt, that
/// would re-initialize FlutterFire and make our application re-enter loading state,
/// which is undesired.
class App extends StatefulWidget 
  // Create the initialization Future outside of `build`:
  @override
  _AppState createState() => _AppState();


class _AppState extends State<App> 
  /// The future is part of the state of our widget. We should not call `initializeApp`
  /// directly inside [build].
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) 
    return FutureBuilder(
      // Initialize FlutterFire:
      future: _initialization,
      builder: (context, snapshot) 
        // Check for errors
        if (snapshot.hasError) 
          return SomethingWentWrong();
        

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) 
          return MyAwesomeApp();
        

        // Otherwise, show something whilst waiting for initialization to complete
        return Loading();
      ,
    );
  

如您所见,该函数在调用 runApp(App()); 之后被调用。 Firebase.initializeApp(); 不仅返回 Future&lt;FirebaseApp&gt;(您的代码中没有 await),它还是主小部件状态的一部分。

【讨论】:

我按照你的建议做了,现在所有错误都消失了,但现在它执行第一个 if statement 条件为 snapshot.hasError 并返回 SomethingWentWrong() 代码 谢谢他现在工作的问题原因是main.dart中的'auth'

以上是关于如何将 Flutter 项目迁移到 Firebase 版本 9 现代 Web 模块化风格的主要内容,如果未能解决你的问题,请参考以下文章

无法将 Flutter 项目迁移到 AndroidX

将 Flutter 应用迁移到 Flutter Web 时如何显示图像资产?

如何将 Flutter 移动应用迁移到 Flutter Web

手把手教你将项目迁移到Flutter2.0 空安全

Flutter:Google Maps 如何从 Firestore 设置图标

如何将 Flutter Bloc 与 Firebase 电话身份验证一起使用