Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

dart - Can I use multiple method on a future builder?

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

question from:https://stackoverflow.com/questions/50626949/can-i-use-multiple-method-on-a-future-builder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  },
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...