Cameron Johnson
Cameron Johnson

Reputation: 163

the method 'setData' isn't defined for the type 'documentreference'

I am currently in the middle of migrating all of my out of date code to the newest version of firebase and when I updated the firebase dependency it gave me the error that the method 'setData' isn't defined for the type 'documentreference'.

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

}

Upvotes: 5

Views: 3651

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

The setData() method was renamed to just set() in version 2.0 of the FlutterFire plugins.

For cases such as this, I find it most helpful to keep the reference documentation handy, such as for the DocumentReference class in this specific case.

Upvotes: 8

Related Questions