title: How to wait for Future method inside widget testing?
body: <p>I want to test my widget. In my HomePage widget, there's a method to call API then it will show the result inside this widget. Here's the code for HomePage widget</p>
  
  <pre class="lang-dart prettyprint-override"><code>class HomePage extends StatefulWidget {
    WebScraper _webScraper = WebScraper();
  
    HomePage();
  
    HomePage.forTest(Client client) {
      _webScraper.client = client;
    }
  
    @override
    _HomePageState createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
    List&lt;Post&gt; posts = [];
  
    bool isPostLoaded = false;
  
    @override
    void initState() {
      super.initState();
      _onRefresh();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Column(
          children: &lt;Widget&gt;[
            Expanded(
              child: RefreshIndicator(
                child: isPostLoaded ? ListView.builder(
                  key: Key('post-list'),
                  itemCount: posts.length,
                  itemBuilder: (context, index) {
                    return Card(
                      key: Key(posts[index].id.toString()),
                      child: Column(
                        children: &lt;Widget&gt;[
                          Text(posts[index].title),
                          Text(posts[index].content),
                          Row(
                            children: &lt;Widget&gt;[
                            // some code
                            ],
                          )
                        ],
                      ),
                    );
                  },
                ) : CircularProgressIndicator(),
                onRefresh: _onRefresh),
            ),
          ],
        ),
      );
    }
  
    Future&lt;void&gt; _onRefresh() async {
      List&lt;Post&gt; postFromWebsite =
          await this.widget._webScraper.getPostsFromWebsite();
      if (postFromWebsite.length &gt; 0) {
        setState(() {
          posts = postFromWebsite;
          isPostLoaded = true;
        });
      }
    }
  </code></pre>
  
  <p>And here is my test code</p>
  
  <pre class="lang-dart prettyprint-override"><code>void main() {
    var homeHttpMock;
    setUp(() {
      MockClient client = MockClient((request) async {
        String html =
            await rootBundle.loadString('test_resources/test.html');
        return Response(html, 200);
      });
  
      homeHttpMock = MediaQuery(
        data: MediaQueryData(),
        child: MaterialApp(
          home: HomePage.forTest(client),
        ),
      );
    });
  
    testWidgets('Show post inside card in the list view',
        (WidgetTester tester) async {
      await tester.pumpWidget(homeHttpMock);
      await tester.pump();
      final listView = find.byKey(Key('post-list'));
      expect(listView, findsOneWidget);
    });
  </code></pre>
  
  <p>I tried to use <code>tester.runAsync</code>, <code>tester.pump</code> with duration, <code>tester.pumpAndSettle</code> (this one will be timed out), and <code>FakeAsync</code> but these methods don't work for my widget test and it will lead my test to be failed.</p>
  
  <p>Thank you in advance</p>