更新 Firebase 插件更新后颤振中的"then"问题



我过去在我的项目中使用过这段代码,它非常适合

FirebaseAuth.instance.currentUser.then((user) {
FirebaseFirestore.instance.collection('todos').doc().set({
'body': theController.text,
'done': false,
'user_id': user.uid,
'time': DateTime.now().toString(),
});
}).then((value) {
Navigator.of(context).pop();
theController.clear();
});

但现在是"不接受";CCD_ 1";错误是"错误";方法"then"不是为类型"User"定义的;

如果您阅读了Firebase Auth Changelog,您将看到以下突破性的更改:

BREAKING:通过currentUser((访问当前用户现在是通过currentUser getter同步的。

这意味着它不再返回Future,而是直接返回用户对象。这意味着您可以简化代码:

final user = FirebaseAuth.instance.currentUser;
FirebaseFirestore.instance.collection('todos').doc().set({
'body': theController.text,
'done': false,
'user_id': user.uid,   'time': DateTime.now().toString(),
}).then((value) {
Navigator.of(context).pop();
theController.clear();
});

最新更新