【问题标题】:problem with "then" in flutter after update the firebase plugins update [duplicate]更新firebase插件更新后颤振中的“then”问题[重复]
【发布时间】:2020-12-12 17:55:44
【问题描述】:

我过去在我的项目中使用过这段代码,它运行良好

    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();
});

但现在不接受“.then” 错误是“没有为“用户”类型定义方法“然后”

【问题讨论】:

  • 检查我链接的所有 3 个重复项,currentUser 现在是一个属性,不会返回 Future

标签: firebase flutter


【解决方案1】:

如果您阅读过Firebase Auth Changelog,您将看到以下重大变化:

中断:通过 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();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-24
    • 2021-10-21
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多