在颤动消防区中使用.Data[]时,操作员定义的错误 [英] "The operator '[]' isn't defined" error when using .data[] in flutter firestore

查看:0
本文介绍了在颤动消防区中使用.Data[]时,操作员定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何按照YouTube上的《网忍者》教程在扑翼中使用Firestore。完成用户身份验证后,每当创建新用户时,此用户都会将用户记录添加到数据库中,为此会添加一个新模型,传递名为&name的1个字符串,根据我理解的调用,他映射了模型,然后使用.Data[‘name’]从模型中获取该字符串(字符串称为name),在执行此操作时,我收到错误The operator '[]' isn't defined for the type 'Map<String, dynamic> Function()'为什么会出现此错误?

用户名模型

class Username {
  final String name;
  Username({ this.name });
}

rabse.dart文件(以下代码包装在一个名为DatabaseService的类中)

  List<Username> _usernameListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc){
      return Username(
        name: doc.data['name'] ?? '',
      );
    }).toList();
  }

Auth.Dart

  Future registerWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      User user = result.user;

      // create a new document for the user with uid
      await DatabaseService(uid: user.uid).updateUserData('user123');
      return _userFromFirebaseUser(user);
    } catch(e) {
      print(e.toString());
      return null;
    }
  }

如果您有任何问题或需要查看更多代码,请在评论中让我知道

推荐答案

更改:

name: doc.data['name'] ?? '' 

进入:

name: doc.data()['name'] ?? '' 

data()现在是一个方法,因此您必须从源代码添加()

  Map<String, dynamic> data() {
    return _CodecUtility.replaceDelegatesWithValueInMap(
        _delegate.data(), _firestore);
  }

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/document_snapshot.dart#L38

这篇关于在颤动消防区中使用.Data[]时,操作员定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆