【问题标题】:the method 'setData' isn't defined for the type 'documentreference'没有为“documentreference”类型定义方法“setData”
【发布时间】:2021-10-12 18:34:16
【问题描述】:

我目前正在将所有过时的代码迁移到最新版本的 firebase,当我更新 firebase 依赖项时,它给了我一个错误,即方法 'setData' 没有为类型定义'文档参考”。

class DatabaseService {

  final String uid;
  DatabaseService({ this.uid });

  // Collection reference
  // final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');
  final CollectionReference<Map<String, dynamic>> brewCollection = FirebaseFirestore.instance.collection('brews');



  Future updateUserData(String sugars, String name, int strength) async {
    return await brewCollection.doc(uid).setData({
      'sugars' : sugars,
      'name'  : name,
      'strength' : strength
    });
  }


  // Brew list from snapshot
  List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc){
      return Brew(
          name: doc.data['name'] ?? '',
          strength: doc.data['strength'] ?? 0,
          sugars: doc.data['sugars'] ?? '0'
      );
    }).toList();
  }

  // Get brews stream
  Stream<List<Brew>> get brews {
    return brewCollection.snapshots().map(_brewListFromSnapshot);
  }

  UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
    return UserData(
        uid: uid,
        name: snapshot.data['name'],
        sugars: snapshot.data['sugars'],
        strength: snapshot.data['strength']
    );
  }

  // Get user document
  Stream<UserData> get userData {
    return brewCollection.doc(uid).snapshots().map(_userDataFromSnapshot);
  }

}

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    setData() 方法在 FlutterFire 插件 2.0 版中被重命名为 set()

    对于此类情况,我发现将参考文档放在手边最有帮助,例如在此特定情况下的 DocumentReference class

    【讨论】:

    • 感谢您的建议,我查看了文档,它帮助了很多!我只是对分配给“Brew”的字符串和整数值有疑问。特别是数据方法内部的值。现在说对象'[]'没有为类型对象函数定义。我确信这只是新的 firebase 更新附带的语法错误,但我不太确定。感谢大家的帮助!
    • 您可能想搜索那个新的错误消息,因为我在过去几个月中看到它弹出 很多,而且修复方法总是非常相似。
    • 感谢您的输入,我发现这个帖子回答了这个问题。 stackoverflow.com/questions/68615606/…
    猜你喜欢
    • 2021-06-06
    • 2021-09-02
    • 2022-11-21
    • 2021-10-19
    • 2020-12-14
    • 2021-07-19
    • 2021-09-01
    • 2021-09-17
    • 2021-06-11
    相关资源
    最近更新 更多