【问题标题】:Where to add Firebase.initializeApp in flutter? [duplicate]在哪里添加 Firebase.initializeApp 在颤振? [复制]
【发布时间】:2021-11-15 06:18:00
【问题描述】:

错误:没有创建 firebase 应用程序调用 firebase.initializeapp 我的问题:我应该在哪里添加 firebase 初始化

带有 Firestore 引用“用户”的无状态小部件

class FeedBack extends StatelessWidget {
  CollectionReference users = FirebaseFirestore.instance.collection('users');
  late String txtnote;
  late String note;
  @override
  Widget build(BuildContext context) {
    return Scaffold(

onpress 集成,用于将数据写入 firestore

child: ElevatedButton(
                    onPressed: () async {
                      await users.add({
                        'subject': note,
                        'email': 'example@gmail.com',
                        'description': txtnote
                      }).then((value) => print('Data Added Successfully!'));
                    },
                    child: Text(
                      'Submit',
                      style: TextStyle(color: Colors.white),
                    ),

注意:这个 dart 文件 'feedback.dart' 不包含 void main 函数,它是一个无状态小部件

【问题讨论】:

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您可以在应用程序的主要入口点内部调用:

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

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 感谢您的帮助。
【解决方案2】:

默认情况下,这应该放在 main.dart 文件中

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

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 非常感谢,现在完全好了。
猜你喜欢
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 2021-06-05
  • 1970-01-01
  • 2012-11-01
  • 2020-05-14
  • 2023-03-18
相关资源
最近更新 更多