3

Say my document like this: -user_id: -name -address -A lot of heavy data.

I know I could get the entire document and just display the fields that I want, but I nedd to retrieve only name and address from my document, so that my users can save bandwidth.

How can I do this?

PD: this is how I would retrieve the whole document.

return StreamBuilder(
  stream: Firestore.instance
      .collection('users')
      .document(widget.userId.toString())
      .snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(
          valueColor: AlwaysStoppedAnimation<Color>(Colors.deepOrange),
        ),
      );
    } else {
      return _buildUserTile(context, snapshot.data);
    }
  },
);
Isaac
  • 1,436
  • 2
  • 15
  • 29
  • All Firebase mobile clients always read the entire document. There is no "projection" operation that filters fields for mobile clients. – Doug Stevenson Oct 30 '18 at 21:20
  • Ok, I see. Anyways, I don't think this is a duplicate question since I'm asking for how to do this on Flutter and you are refering to an ios question. I mean, I did't check that question because the title specify that it is an ios problem. – Isaac Oct 31 '18 at 10:03
  • Because the answer is generic to all platforms, and @Doug Stevenson is one of the Firebase advocates, so you can count on his word :) – salihgueler Oct 31 '18 at 10:16
  • 1
    I trust him and agree. The answer is the same, but not the question, and stackoverflow ask for duplicate questions. "This question has been asked before and already has an answer". I think this is not true. A flutter dev search for flutter or generic questions but neither this or the other one are generic IMHO (I'm talking about the question not the answer). But anyway this was just my opinion, leave like this if you want – Isaac Oct 31 '18 at 11:51

1 Answers1

17

Official answer from a Firebase Engineer

Cloud Firestore always reads and returns full documents. There is no way to read a subset of the fields in a document.

You can retrieve the entire document, and then process the DocumentSnapshot to just use the fields you're interested. But this means you're using more bandwidth than needed. If this is a regular occurrence for your app, consider creating a secondary collection where each document contains just the fields you're interested in.

salihgueler
  • 3,284
  • 2
  • 22
  • 33