Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Streambuilder returning loop

I have data from my firebase and to simplify my code I would like to return a loop of all elements. Here is my Streambuilder:

Column(
      children: <Widget>[
        Container(
          height: 300,
          width: double.infinity,
          child: Image.network(
            widget.image,
            fit: BoxFit.cover,
          ),
        ),
        StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection(
                    '/towns/${widget.townId}/beacons/${widget.beaconId}/content')
                .snapshots(),
            builder: (ctx, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting)
                return CircularProgressIndicator();
              final contents = snapshot.data.docs;
              for (int i = 1; i <= 10; i++) {
                return Column(
                  children: [
                    contents[1]['sectionTitle$i'].isNotEmpty
                        ? buildSectionTitle(
                            context, contents[1]['sectionTitle$i'])
                        : Container(),
                    contents[1]['text$i'].isNotEmpty
                        ? buildText(context, contents[1]['text$i'])
                        : Container(),
                    contents[1]['audio$i'].isNotEmpty
                        ? Audio(contents[1]['audio$i'], key: UniqueKey())
                        : Container(),
                    contents[1]['image$i'].isNotEmpty
                        ? buildImage(context, contents[1]['image$i'])
                        : Container(),
                  ],
                );
              }
              return Container();
            }),
      ],
    ),

In my firebase, I have all the elements (sectionTitle1, sectionTitle2, etc.) are on the same document (here contents[1]). The problem is the Streambuild only returns the firsts elements of my loop. For example, if I start to i = 1, the loop will return sectionTitle1, text1, audio1 and image1 ; if I start to i = 3, the loop will return sectionTitle3, text3, audio3 and image3. How can I return everythings ?

like image 757
johnsmith2020 Avatar asked Apr 22 '26 15:04

johnsmith2020


1 Answers

Updated answer

You could try this way, it may solve your issue:

return Column(
  children: [
    for (int i = 1; i <= 10; i++) ...[
      contents[1]['sectionTitle$i'].isNotEmpty
        ? buildSectionTitle(
          context, contents[1]['sectionTitle$i'])
        : Container(),
      contents[1]['text$i'].isNotEmpty
        ? buildText(context, contents[1]['text$i'])
        : Container(),
      contents[1]['audio$i'].isNotEmpty
        ? Audio(contents[1]['audio$i'], key: UniqueKey())
        : Container(),
      contents[1]['image$i'].isNotEmpty
        ? buildImage(context, contents[1]['image$i'])
        : Container(),
    ]
  ]
);

This way you won't have to create a Column for each iteration. and also remove the return Container(); at the end

Previous wrong answer

you are using content[1] instead of content[i] so you will always have the result of docs[1] in your loop

like image 108
Arnaud Delubac Avatar answered Apr 25 '26 15:04

Arnaud Delubac