繁体   English   中英

当 firestore 中有数据时检索到 null flutter

[英]Retrieved null when there is data in firestore with flutter

我正在开发一个应用程序的配置文件页面,该页面应该从 firestore 检索数据并将它们显示在可编辑的文本字段或日期选择器或其他内容中。

原来snapshot.data好像不是null,但是当我的firestore里面有数据的时候,它得到的字段是null。

这是我的代码:

  @override
  initState() {
    super.initState();
    initInfo();
  }

  initInfo() async {
    // get info from current user
    final currUser = await FirebaseAuth.instance.currentUser;
    if (currUser != null) {
      DocumentSnapshot snapshot = await FirebaseFirestore.instance
          .collection('users')
          .doc('Dv7DkLm6Ls8Q4tKuVXEq')
          .get();

      Map<String, dynamic> data = snapshot.data()! as Map<String, dynamic>;
      name = data['name'];
      profilePath = data['profilePath'];
      birthday = data['birthday'].toDate();
      anniversary = data['annversary'].toDate();
      couple = data['couple'];

      setState(() {});
    }
  }

错误类似于:[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: NoSuchMethodError: The method 'toDate' was called on null。

收件人:null

尝试调用:toDate()

#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)

#1 _UserPageState.initInfo

而我的 firebase 就像: firebase

您能否借助 log(data.toString()) 或 print(data.toString()) 检查数据 map 中获取的数据?

var birthday;
var anniversary;

  birthday = data['birthday'].toDate() ?? DateTime.now();
  anniversary = data['annversary'].toDate() ?? DateTime.now();

1st 你需要检查birthdayanniversary变量类型使其成为 var。 不是字符串或日期时间,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM