英文:
The argument type 'Null Function(DataSnapshot)' can't be assigned to the type 'FutureOr<dynamic> Function(DatabaseEvent)' in v 4.6.1
问题
I needed to check if the firebase database had the user,
if (firebaseUser != null) {
usersRef
.child(firebaseUser.uid)
.once()
.then((DataSnapshot snap){
if (snap.value != null)
{
Navigator.pushNamedAndRemoveUntil(
context, HomeScreen.idScreen, (route) => false);
displayToastMessage("You are logged-in now", context);
} else {
_firebaseAuth.signOut();
displayToastMessage(
"No records found. Create a new account", context);
}
});
}
I added as FutureOr Function(DatabaseEvent)); yet I
I got unhandled exception when I run the code. I would be glad if someone has also encountered a similar error and could help.
英文:
I needed to check if the firebase database had the user,
if (firebaseUser != null) {
usersRef
.child(firebaseUser.uid)
.once()
.then((DataSnapshot snap){
if (snap.value != null)
{
Navigator.pushNamedAndRemoveUntil(
context, HomeScreen.idScreen, (route) => false);
displayToastMessage("You are logged-in now", context);
} else {
_firebaseAuth.signOut();
displayToastMessage(
"No records found. Create a new account", context);
}
});
}
I added as FutureOr Function(DatabaseEvent)); yet I
I got unhandled exception when I run the code. I would be glad if someone has also encountered a similar error and could help.
答案1
得分: 1
我认为错误是因为 Firebase 数据库 API 在 'onValue' 事件处理程序中进行了更新。现在 'once()' 方法提供了一个 'DatabaseEvent' 而不是 'DataSnapshot'。
你可以尝试这样做:
if (firebaseUser != null) {
usersRef
.child(firebaseUser.uid)
.get()
.then((DatabaseEvent event) {
DataSnapshot snap = event.snapshot;
if (snap.value != null) {
Navigator.pushNamedAndRemoveUntil(context, HomeScreen.idScreen, (route) => false);
displayToastMessage("你已成功登录", context);
} else {
_firebaseAuth.signOut();
displayToastMessage("未找到记录。请创建一个新账户", context);
}
});
}
在这个修改后的代码片段中,回调函数接收到了 'DatabaseEvent event'。然后从 'event.snapshot' 中提取出了 'DataSnapshot snap'。其余的代码保持不变。
此解决方案假设你使用的是 Firebase 数据库 API 的 4.6.1 版本,正如你在问题中提到的。
英文:
I think the error is due to the fact Firebase Database API got an update in the 'onValue' event handler. the 'once()' method is now providing a 'DatabaseEvent' instead of a 'DataSnapshot'.
You could try this instead:
if (firebaseUser != null) {
usersRef
.child(firebaseUser.uid)
.get()
.then((DatabaseEvent event) {
DataSnapshot snap = event.snapshot;
if (snap.value != null) {
Navigator.pushNamedAndRemoveUntil(context, HomeScreen.idScreen, (route) => false);
displayToastMessage("You are logged-in now", context);
} else {
_firebaseAuth.signOut();
displayToastMessage("No records found. Create a new account", context);
}
});
}
In this revised snippet, 'DatabaseEvent event' is provided to the callback function for the 'then' method. The 'DataSnapshot snap is' then extracted from the 'event.snapshot'. The rest of the code remains the same.
This solution assumes you are using version 4.6.1 of the Firebase Database API as per your question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论