bernardohrl
bernardohrl

Reputation: 11

Flutter: How to make a sequence of http requests on a widget before build method

I have 3 classes: Users, Posts and Comments. User has many Posts and Posts has many Comments.

I want that all data to be fetched before the widget's build method is called.

I tryed to use initState() to do this:

class FetchDataExample extends StatefulWidget {
  final User _user;

  FetchDataExample(this._user);

  @override
  _State createState() => _State(_user);
}

class _State extends State<FetchDataExample> {
  final User _user;

  _State(this._user);

  @override
  void initState() {
    _user.setPosts();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print(this._user.posts[0]);


    return Container(

    );
  }
}

In User class I have:

void setPosts() async {

    String url = 'https://jsonplaceholder.typicode.com/posts?userId=' + this.id.toString();
    var request = Requester.get(url); // Returns a Future<Response>

    await request.then((value) => this.posts = Post.jsonToPosts(json.decode(value.body)));

    this.posts.forEach((post) => post.setComments());

    print(this.posts[0]);
}

The 'setComments()' has the same logic.

I have two prints:

  1. Inside build that returns null;
  2. Inside setPosts the returns Instance of 'Post';

So, by the time that Build method is called in the widget, the initState has not finished yet.

I need it be finished, does anyone know how can I do that?

Upvotes: 1

Views: 1160

Answers (1)

HasilT
HasilT

Reputation: 2609

You can use a FutureBuilder to build a widget by using latest result from a future. And also you can combile multiple futures into a single one using Future.wait method.

Here is a sample code:

_getPageData() async {
  var _combinedFutures =  await Future.wait([setPosts, setComments]);
  //do stuff with data
}
...
@override
  Widget build(BuildContext context) {
    return FutureBuilder(
         future:_getPageData(),
         builder: (context, snapshot) {
            return Container();
         }),
    );

  });

Upvotes: 2

Related Questions