Can I use multiple method on a future builder?

DartFlutter

Dart Problem Overview


     @override
      Widget build(BuildContext context) {
        widget.groupid;
        widget.event_id;
        var futureBuilder = new FutureBuilder(
          future: _getAllTickets(),
    
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            print(snapshot.connectionState);
            switch (snapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return new Text('...');
              default:
                if (snapshot.hasError)
                  return new Text('Error: ${snapshot.error}');
                else
                  return createListTickets(context, snapshot);
            }
          },
        );



    return new Scaffold(

      body: futureBuilder,

    );
  }

  Widget createListTickets(BuildContext context, AsyncSnapshot snapshot) {
    List values = snapshot.data;

     child: new Card(

                child:
                new Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
                  new Text(
                      values[index]["ticket_type_id"].toString(), style:
                  const TextStyle(
                      fontFamily: 'Poppins',
                      fontWeight: FontWeight.w600,
                      fontSize: 25.0)),
}


  _getAllTickets() async {
    final response = await http.get(
        "https...}"
        , headers: {
      HttpHeaders.AUTHORIZATION: access_token
    });

    returnTickets = json.decode(response.body);
    return returnTickets;
  }


  _getTicketType() async {
    for (i = 0; i < (returnTickets?.length ?? 0); i++) {
      /*print("https....);*/
     final responseType = await http.get(
          "https...}"
          , headers: {
        HttpHeaders.AUTHORIZATION: access_token
      });

      Map<String, dynamic> hey = json.decode(responseType.body);
      

    }



Hi everyone, I have a question. As I am sending multiple API request and building dynamically a card with the response that I get in return, I was wondering if I could include more that one method within future: _getAllTickets(), + (another method), as I would like to substitute values[index]["ticket_type_id"] with values[index]["name"], which name is a new index response that I have got through the method _getTicketType(). Thank you in advance!

Dart Solutions


Solution 1 - Dart

You can use Future.wait(Future[]) to return a list of futures.

Future<String> foo;
Future<int> bar;
FutureBuilder(
  future: Future.wait([bar, foo]),
  builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
    snapshot.data[0]; //bar
    snapshot.data[1]; //foo
  },
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionheyrView Question on Stackoverflow
Solution 1 - DartRémi RousseletView Answer on Stackoverflow