Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use multiple method on a future builder?

Tags:

flutter

dart

     @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!

like image 711
heyr Avatar asked May 31 '18 14:05

heyr


People also ask

Why is FutureBuilder called multiple times?

Why FutureBuilder Called again? This is due to state changes that come if setState() called, which triggers build() method, and due to this inside widgets will be re-initialize again.

When should I use future builder flutter?

In Flutter, the FutureBuilder Widget is used to create widgets based on the latest snapshot of interaction with a Future. It is necessary for Future to be obtained earlier either through a change of state or change in dependencies.

How do you wait for a future to complete a flutter?

wait<T> method Null safety Returns a future which will complete once all the provided futures have completed, either with their results, or with an error if any of the provided futures fail.


1 Answers

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   }, ); 
like image 129
Rémi Rousselet Avatar answered Sep 22 '22 19:09

Rémi Rousselet