oividiosCaeremos
oividiosCaeremos

Reputation: 748

OrderBy Statement for Subfields on Firebase in Flutter

I have a collection of following;

firebase

I want to return every company in ascending order by companyName and this is how I'm trying;

QuerySnapshot usersComp = await _firestore
        .collection('companies')
        .where('belongsTo', isEqualTo: curruser.uid)
        .orderBy({
      'properties': {'companyName'}
    }, descending: false).getDocuments();

Code doesn't give any errors but it also doesn't return any value as well. The statement without orderBy works fine.

How can I write a this orderBy in a working way?

edit

This is the whole getCompanies function in FirebaseCrud:

Future<List<Company>> getCompanies() async {
    FirebaseUser curruser = await _authService.getCurrentUser();

    DocumentSnapshot userSnapshot = await Firestore.instance
        .collection('users')
        .document(curruser.uid)
        .get();

    List partners = userSnapshot.data['partners'];
    print('partners');
    print(partners[0]);

    QuerySnapshot usersComp = await _firestore
        .collection('companies')
        .where('belongsTo', isEqualTo: curruser.uid)
        //.orderBy('properties.companyName', descending: false) // code works fine without this line but not as expected
        .getDocuments();

    List<Company> companies = new List<Company>();

    usersComp.documents.forEach((f) {
      int indexOfthis = usersComp.documents.indexOf(f);

      companies.add(new Company(
        uid: partners[indexOfthis],
        companyName: f.data['properties']['companyName'],
        address: f.data['properties']['address'],
        paymentBalance: f.data['currentPaymentBalance'],
        revenueBalance: f.data['currentRevenueBalance'],
        personOne: new Person(
            phoneNumber: f.data['properties']['personOne']['phoneNumber'],
            nameAndSurname: f.data['properties']['personOne']
                ['nameAndSurname']),
        personTwo: new Person(
            phoneNumber: f.data['properties']['personTwo']['phoneNumber'],
            nameAndSurname: f.data['properties']['personTwo']
                ['nameAndSurname']),
      ));
    });

    return companies;
  }

Upvotes: 1

Views: 1257

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599976

I created a collection with a few documents, each have a top-level field and a nested field. If I then run a query over it like this:

Firestore.instance.collection("59739861")
    .where("location", isEqualTo: "Bay Area")
    .orderBy("properties.companyName", descending: false)
    .getDocuments().then((querySnapshot) {
      print("Got ${querySnapshot.documents.length} documents");
      querySnapshot.documents.forEach((doc) {
        print("${doc.documentID}: ${doc.data['properties']}");
      });
    });

I have added these three documents in my database:

  • location: "Bay Area", properties.companyName: "Google"
  • location: "Bay Area", properties.companyName: "Facebook"
  • location: "Seattle", properties.companyName: "Microsoft"

With the above query, I get the following output printed:

flutter: KJDLLam4xvCJEyc7Gmnh: {companyName: Facebook}

flutter: TMmrOiKTYQWFBmJpughg: {companyName: Google}

I have no idea why you're getting different output.

Just in case it matters, I run the app in an iOS simulator and use this version of the Firestore plugin:

cloud_firestore: ^0.13.0+1

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317818

Use dot notation to reference properties of objects to use for sorting.

_firestore
    .collection('companies')
    .where('belongsTo', isEqualTo: curruser.uid)
    .orderBy('properties.companyName'),
    descending: false)

Upvotes: 2

Related Questions