【问题标题】:New Firebase connection to Flutter application not connecting与 Flutter 应用程序的新 Firebase 连接未连接
【发布时间】:2022-10-06 20:15:25
【问题描述】:

我刚刚创建了一个新的颤振应用程序并尝试连接一个新制作的 Firebase Firestore。添加了 Cli 并运行了 flutterfire 配置。

import \'package:flutter/material.dart\';
import \'package:firebase_core/firebase_core.dart\';
import \'firebase_options.dart\';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform
  );
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FutureBuilder(builder: ((context, snapshot) {
        if(snapshot.hasError){
          print(\'Error\');
        }
        if(snapshot.connectionState == ConnectionState.done){
          return const MyHomePage(title: \'WOWZA\',);
        }
        return CircularProgressIndicator();
      }
      )
      )
    );
  }
}

在 if(snapshot.connectionState == ConnectionState.done) 中断时说 connectionState 是 .none 并且不确定原因。

任何人都可以帮忙吗?

  • 我认为你需要为你的 FutureBuilder 添加一个未来

标签: flutter firebase google-cloud-firestore flutter-web


【解决方案1】:

缺少未来的初始化 -

从:

home: FutureBuilder(builder: ((context, snapshot) {

    if(snapshot.hasError){
      print('Error');
    }
    if(snapshot.connectionState == ConnectionState.done){
      return const MyHomePage();
    }
    return CircularProgressIndicator();
  }
  )
  )

至:

home: FutureBuilder(
        future: _initialization,
        builder: ((context, snapshot) {
        if(snapshot.hasError){
          print('Error');
        }
        if(snapshot.connectionState == ConnectionState.done){
          return const MyHomePage();
        }
        return CircularProgressIndicator();
      }
      )
      )

【讨论】:

    【解决方案2】:

    用这个 sn-p 修改你的代码。

    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: FutureBuilder(
            future: Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform
      ),
            builder: ((context, snapshot) {
            if(snapshot.hasError){
              print('Error');
            }
            if(snapshot.connectionState == ConnectionState.done){
              return const MyHomePage(title: 'WOWZA',);
            }
            return CircularProgressIndicator();
          }
          )
          )
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 2021-04-07
      • 2020-03-23
      • 2021-07-08
      • 2021-12-12
      • 2021-09-04
      • 2020-06-08
      • 2023-02-20
      • 2017-04-30
      相关资源
      最近更新 更多