在Flutter中何处添加Firebase.initializeApp?[duplicate]

nkcskrwz  于 2022-11-30  发布在  Flutter
关注(0)|答案(2)|浏览(160)
    • 此问题在此处已有答案**:

No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase(27个答案)
去年关闭了。
错误:没有firebase应用程序已创建调用firebase. initializeapp我的问题:我应该在哪里添加firebase初始化
具有firestore引用"users"的无状态小部件

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

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

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函数,它是一个无状态的小部件

g6ll5ycj

g6ll5ycj1#

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

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

6g8kf2rb2#

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

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

相关问题