Stream builder from firestore to flutter

前端 未结 3 605
花落未央
花落未央 2020-12-05 20:41

I am wondering how to get data from firestore to flutter app using the streambuilder. I created the necessary Boilerplate code I have the widget built and working and in the

相关标签:
3条回答
  • 2020-12-05 21:18
    Card buildItem(DocumentSnapshot doc) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              'name: ${doc.data['name']}',
              style: TextStyle(fontSize: 24),
            ),
            Text(
              'todo: ${doc.data['todo']}',
              style: TextStyle(fontSize: 20),
            ),
            Text(
              'Age: ${doc.data['age']}',
              style: TextStyle(fontSize: 10),
            ),
            SizedBox(
              height: 12,
            ),
              ],
            )
          ],
        ),
      ),
    );  }
    

    For other persons who will face the same problem, the card and stream builder will represent a solution. The Widget has the Card just before it declaration and has inside the body the next part:

     body: ListView(
            padding: EdgeInsets.all(8),
            children: <Widget>[
              Form(
                key: _formKey,
                child: buildTextFormField(),
              ),
              StreamBuilder<QuerySnapshot>(
                stream: db
                    .collection('CRUD')
                    .snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Column(
                        children: snapshot.data.documents
                            .map((doc) => buildItem(doc))
                            .toList());
                  } else {
                    return SizedBox();
                  }
                },
              )
            ],
          ),
    
    0 讨论(0)
  • 2020-12-05 21:19

    This should work for one item

      body: new StreamBuilder(
        stream: Firestore.instance.collection("collection").snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Text(
              'No Data...',
            );
          } else { 
              <DocumentSnapshot> items = snapshot.data.documents;
    
              return new Lost_Card(
              headImageAssetPath : items[0]["url"]
              );
          }
    

    If you want to create list builder from many documents use it like this

            return new ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  DocumentSnapshot ds = snapshot.data.documents[index];
                  return new Lost_Card(
                    headImageAssetPath : ds["url"];
    
    );
    
    0 讨论(0)
  • 2020-12-05 21:30
    StreamBuilder<List<UData>>(
                  stream: AdminData().getDrivers,
                  builder: (context, snapshot) {
                    return ListView(
                      children: snapshot.data.map((document) {
                        return hadCard(
                          widget: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              hadText(title: document.name),
                              hadText(title: document.phone),
                              hadText(title: document.Driver),
                            ],
                          ),
                        );
                      }).toList(),
                    );
                  }),
    
    0 讨论(0)
提交回复
热议问题