【问题标题】:Cannot get a field on DocumentSnapshotPlatform which does not exist无法获取 DocumentSnapshotPlatform 上不存在的字段
【发布时间】:2021-09-28 18:26:48
【问题描述】:

我正在尝试向应用栏显示名称,但用户 ID 部分似乎给出了错误。我是颤振和 Firestore 的初学者,有人可以帮助我吗

class Profile extends StatefulWidget {
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  //To retrieve the Userid
  User? user;
  Future<void> getUserData() async {
    User userData = await FirebaseAuth.instance.currentUser!;
    setState(() {
      user = userData;
      print(userData.uid);
    });
  }
///////////////////////////////////////

  Future<String>? _title;
  @override
  void initState() {
    getUserData().then((value) => _title = _getAppBarNameWidget());
    // _title = _getAppBarNameWidget();
    super.initState();
  }

//To retrieve the name from firestore
  Future<String> _getAppBarNameWidget() async =>
      await FirebaseFirestore.instance
          .collection('customer')
          .doc(user!.uid)
          .get()
          .then((DocumentSnapshot ds) async {
        var name = ds['name'];
        return name;
      });

【问题讨论】:

  • 你能显示你的错误信息吗
  • StateError(错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段)
  • 你确定你输入了正确的键名
  • 是的 .doc(user!.uid) 如果我给 .doc('Xk1eUAfdSMqp7rpwYxc4') 这样的直接 uid 它可以工作
  • inside setState when r print is it uid is print

标签: flutter google-cloud-firestore


【解决方案1】:

DocumentSnapshot ds不直接包含文档数据,只有文档id,你必须使用data()函数来获取数据。检查文档是否真的存在也是一个好主意,因为如果找不到文档,您将获得快照事件。

例子:

ds.exists          // will return true if document is found
ds.id              // will return document reference
ds.data()!['name'] // will return 'name' field of document

【讨论】:

  • 没有为“对象”类型定义运算符“[]”。尝试定义运算符 '[]' 。我收到 3e=rd 的这个错误
  • 感谢我有 2 个收藏
  • 好的,很高兴听到。如果它是一个对象(例如,如果您使用 fromJson 转换器),则只需将其与点一起使用:.name 而不是['name']
猜你喜欢
  • 2021-08-15
  • 2021-10-26
  • 2021-05-16
  • 2021-05-19
  • 2021-11-03
  • 2021-04-27
  • 2021-06-17
  • 1970-01-01
  • 2023-01-11
相关资源
最近更新 更多