mame
mame

Reputation: 109

Loop in FutureBuilder doesnt work properly

I want to go through a loop in FutureBuilder. Everytime snapshot.data.berichte[i].team == team, I want to return the Card with its content. But only the first Card gets returned. So i++ does not get executed.

return FutureBuilder<BerichtList>(
        future: futureBerichte,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            for (var i = 0; i < snapshot.data.berichte.length; i++) {
              if (snapshot.data.berichte[i].team == team) {
                print(i);
                return Container(
                  width: double.infinity,
                  height: double.infinity,
                  margin: EdgeInsets.all(15.0),
                  child: Card(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(15.0)),
                    elevation: 5,
                    color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.symmetric(vertical: 15),
                          child: Text(
                            DateFormat("dd.MM.yyyy HH:mm").format(
                                DateTime.parse(
                                    snapshot.data.berichte[i].spielDatum)),
                            style: TextStyles.body,
                          ),
                        ),
                        Container(
                          child: Text(
                            snapshot.data.berichte[i].spiele,
                            style: TextStyles.body,
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(bottom: 15),
                          child: Text(
                            snapshot.data.berichte[i].ergebnis,
                            style: TextStyles.body,
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(bottom: 15),
                          /*   width: 350,
                              height: 800, */
                          child: Text(
                            snapshot.data.berichte[i].ueberschrift,
                            style: TextStyles.headline4,
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(bottom: 30),

                          /*   width: 350,
                              height: 800, */
                          child: Text(
                            snapshot.data.berichte[i].bericht,
                            style: TextStyles.body,
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              } else {}
            }
            return Container();
          } else
            return Center(child: AppProgressIndicator());
        });

Upvotes: 0

Views: 220

Answers (1)

Abbasihsn
Abbasihsn

Reputation: 2171

it is because you are returning the value directly from the FutureBuilder widget, and it completes the function, so the next index does not follow up! you should put your data in a parent widget like Column or ListView and then return it. for example:

return ListView(
   children: snapshot.data.berichte.map(item=>{
      if(item.team==team){
         return YOUR WIDGET
      }
     }).toList();
);

Upvotes: 1

Related Questions