The method 'document' isn't defined for the type 'CollectionReference'. Flutter/Firebase

1,945

CollectionReference in cloud_firestore for flutter/dart does not have a document(..) method. It is called doc(..) instead. https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference/doc.html

Share:
1,945
Nisa Efendioglu
Author by

Nisa Efendioglu

Updated on November 27, 2022

Comments

  • Nisa Efendioglu
    Nisa Efendioglu over 1 year

    The method 'document' isn't defined for the type 'CollectionReference'. Try correcting the name to the name of an existing method, or defining a method named 'document'.

    I am getting this error in my project. Can you help me?

    My Code:

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:crypto_app/models/users.dart';
    
    class FireStoreService {
      final FirebaseFirestore _firestore = FirebaseFirestore.instance;
      final DateTime time = DateTime.now();
    
      Future<void> createUser({id, mail, userName, photoUrl = ""}) async {
        await _firestore.collection("users").doc(id).set({
          "userName": userName,
          "mail": mail,
          "photoUrl": photoUrl,
          "about": "",
          "createTime": time
        });
      }
    
      Future<Users> bringUser(id) async {
        DocumentSnapshot doc = await _firestore.collection("users").doc(id).get();
        if (doc.exists) {
          Users users = Users.dokumandanUret(doc);
          return users;
        }
        return null;
      }
    
      void userUpdate({String userId, String userName, String photoUrl = ""}) {
        _firestore
            .collection("users")
            .document(userId)
            .updateData({"userName": userName, "photoUrl": photoUrl});
      }
    }