在flutter中哪里添加Firebase.initializeApp?

NIK*_* PJ 5 firebase flutter google-cloud-firestore

错误:没有创建 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(
Run Code Online (Sandbox Code Playgroud)

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),
                    ),
Run Code Online (Sandbox Code Playgroud)

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

小智 8

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

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)