Marc Köhler
Marc Köhler

Reputation: 83

Passing Future<List<int>> into snapshot in Button

I am using a FutureBuilder that has a ListView.builder with an itemCount of 3, so that it will display three OutlineButton that carry a String from Firestore that I retrieve via a Future<List<int>>. The list basically has three integers that are randomly generated but meet certain criteria. The values are fine.

return FutureBuilder(
        future: getAllWords(args.collectionId),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          }
          return ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: 3,
              itemBuilder: (context, index) {

                //Future that returns a list of 3 integers
                getRestRandom(args.collectionId).then(handler); 
                int handler(picked){
                  return picked;
                }

                    return ButtonBar(
                      children: <Widget>[
                        OutlineButton(
                            child: Text(snapshot
                                .data[picked] // 1st, 2nd, and 3rd value from List
                                .data["tagalog"]),
                            onPressed: checkCorrect
                                ? () {
                                    setState(() {

                                    });
                                  }
                                : () {
                                    print("FALSE");
                                  })
                      ],
                );
              });
        });

The line getRestRandom(args.collectionId).then(handler); returns an error that says "Local variable 'handler' can't be referenced before it is declared.

What is the best way to feed the List into the OutlineButton text?

Upvotes: 0

Views: 168

Answers (1)

Mazin Ibrahim
Mazin Ibrahim

Reputation: 7889

Rather than using then to wait for getRestRandom(), you can use a nested FutureBuilder to wait for the list generation and then using it to populate your ListView:

 return FutureBuilder(
    future: getAllWords(args.collectionId),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return CircularProgressIndicator();
      }
      return FutureBuilder<List<int>>(
        future: getRestRandom(args.collectionId),
        builder: (context, numbersSnapshot){
          if (!numbersSnapshot.hasData) {
            return CircularProgressIndicator();
          }
          return ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: 3,
              itemBuilder: (context, index) {
                return ButtonBar(
                  children: <Widget>[
                    OutlineButton(
                        child: Text(snapshot
                            .data[numbersSnapshot.data[index]] // 1st, 2nd, and 3rd value from List
                            .data["tagalog"]),
                        onPressed: checkCorrect
                            ? () {
                          setState(() {

                          });
                        }
                            : () {
                          print("FALSE");
                        })
                     ],
                  );
              });
            }
         );
      });

Upvotes: 1

Related Questions