在flutter中向firebase Firestore添加数据后打印语句不执行

问题描述 投票:0回答:1

我正在学习 Firebase 查询,在这里我发现触发查询后它不会在控制台中显示打印消息。

这是我用于演示目的的编码。

我只想向集合中添加一条记录,完成后我希望在控制台中显示成功消息。

return  Scaffold(
      floatingActionButton: FloatingActionButton(onPressed: ()async{
        await FirebaseFirestore.instance.collection('users').add({'Name':'David'});
        print('Success');// this statement does not execute..not showing print on console


      },child: const Icon(Icons.add),),
      body: const Center(child: Text('My  App'),),
    );
flutter google-cloud-firestore
1个回答
1
投票

如果打印未执行,则 Firebase 函数中存在错误或未调用该函数。

在函数开头添加

print
语句以确保调用它,

并尝试将 Firebase 函数包装在 try-catch 块中以查看错误是什么,

像这样:

() async {
   print("function called");
   try {
      await FirebaseFirestore.instance.collection('users').add({'Name':'David'});
   }
   catch (e) {
      print(e);
   }
   print('Success');


}
© www.soinside.com 2019 - 2024. All rights reserved.