我正在学习 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'),),
);
如果打印未执行,则 Firebase 函数中存在错误或未调用该函数。
在函数开头添加
print
语句以确保调用它,
并尝试将 Firebase 函数包装在 try-catch 块中以查看错误是什么,
像这样:
() async {
print("function called");
try {
await FirebaseFirestore.instance.collection('users').add({'Name':'David'});
}
catch (e) {
print(e);
}
print('Success');
}