1

I got a problem. When I want to sort documents with Firestore, I need to return the Query type. So, when I return this type in the addNote method, I get an error in .doc Tell me how to write a method correctly to get rid of this error?

firestore_repository

class Database {
  Query<Map<String, dynamic>> getMainCollection() {
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    return firestore.collection('notes').orderBy('date');
  }

  Future<void> addNote(
    String name,
    String title,
    String? date,
  ) {
    Query<Map<String, dynamic>> mainCollection = getMainCollection();
    return mainCollection
        .doc()
        .set({'name': name, 'title': title, 'date': date})
        .then((value) => print('Note Added'))
        .catchError((error) => print('Failed to add note: $error'));
  }

enter image description here

Max
  • 1,141
  • 2
  • 11
  • 34
  • I think this would help https://firebase.flutter.dev/docs/firestore/example – Trần Trung Hiếu Mar 13 '22 at 22:30
  • [Query class](https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/Query-class.html) does not contain a method '.doc'. Scroll through the various methods you can use in 'Query class'. To write to a document in Firestore you need to know the complete, exact path of that document. You'll need to: 1. Execute the query. 2. Loop over the results. 3. Set data in each document in turn. Something like [this](https://stackoverflow.com/a/62621118/15803365) – Priyashree Bhadra Mar 14 '22 at 07:19

1 Answers1

2

If you are trying to update multiple documents present in a collection.

I think this code might work for you:

Edited:

Make sure that cloud firestore package is the updated one in pubspec.yaml.

class Database {

  FirebaseFirestore firestore = FirebaseFirestore.instance;

  Future<void> addNote(String name,
      String title,
      String? date,) async {
    await firestore.collection('notes').orderBy('date').get().then((snapshot) {
      snapshot.docs
          .forEach((documentSnapshot) async {
        //There must be a field in document snapshot that represents this doc Id
        String thisDocId = documentSnapshot['docId'];
        await firestore.collection('notes').doc(thisDocId).update(
            {
              'name': name,
              'title': title,
              'date': date
            }
        );
      });
    });
  }
}
Bilal Saeed
  • 2,092
  • 8
  • 31
  • this is an update, as I understand it, the same principle and do the creation of a document and deletion? – Max Mar 14 '22 at 18:59
  • Still error: A value of type 'Future>>' can't be returned from the method 'getMainCollection' because it has a return type of 'QuerySnapshot – Max Mar 14 '22 at 19:06
  • This is fine, only 1 more error remains, as it was originally: The getter 'docs' isn't defined for the type 'Future>>'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'. – Max Mar 15 '22 at 08:17