简体   繁体   中英

How to query on Firebase -- Flutter

I'm developing an app that sellers are able to add itens (iPhone, freezer, etc.) and users should be able to query this itens. I'm not bonded to some organizational way - i've tryied creating subcollections (sellers->docId->items->ID->items) and through array (sellers->docId->(..){itens}), main image

Well, I do know how to work with Streambuilder to display and this kinda of stuff. A part of my code is pretty much like this:

StreamBuilder(

                  stream:
                    Firestore.instance.collection('vendedores')
                    .where(['item'],
                    isEqualTo: widget.busca, 
                    ).
                    snapshots(),
                      //Firestore.instance.collection("vendedores").snapshots(),
                  builder: (context, snapshot) {
                    switch (snapshot.connectionState) {
                      case ConnectionState.none:
                      case ConnectionState.waiting:
                        return Center(
                          child: CircularProgressIndicator(),
                        );
                      default:
                      return 
                        Expanded(child:
                         ListView.builder(
                            itemCount: snapshot.data.documents.length,
                            itemBuilder: (context, index) => showCard(
                                context, snapshot.data.documents[index])));
                    }
                  }
              ),

EDIT: I want to be able to find a item (iPhone, freezer) that belongs to a seller, so it should display the seller's name on my card (showCard). So, if there's 5 people selling the same item (ex: Chair), when a user search for "chair", all of 5 sellers will appear in the ListView.

My widget showCard already works - although only as a draft as of right now - and it's basically:

Widget showCard(context, DocumentSnapshot document) {
  return Card(
    elevation: 8,
    child: InkWell(
      splashColor: Colors.orange.withAlpha(30),
      onTap: () {},
      child: Container(
          color: Color(0xffDFE0DF).withOpacity(0.7),
          width: 500,
          height: 120,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: <Widget>[
              Padding(
                  padding: EdgeInsets.only(top: 5, right: 5),
                  child: Text( 
                    document['nomeLoja'],
                    style: TextStyle(fontSize: 20),
                  )),
            ],
          )),
    ),
  );
}

*whereas nomeLoja is the seller's name.

Thanks in advance!

If you change you database structure to something like this:

products: {

    100: {

        name: 'chair',
        vendor: {
            name: 'vendor 1'
        }
    },

    101: {

        name: 'chair',
        vendor: {
            name: 'vendor 2'
        }
    },

    102: {

        name: 'table',
        vendor: {
            name: 'vendor 1'
        }
    }
}

You can query for the data you want with this line:

 final documents = await Firestore.instance.collection('products').where('name', isEqualTo: 'chair').getDocuments();

This way you have the info about the vendor you want and can set the names in a list. Ex:

for(final vendorDocument in documents) {
    final vendor = Vendor.fromData(vendorDocument.data);
    //do what you want with the vendor...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM