Flutter Test - Check widgets after first setState

97 views Asked by At

I have an action taken on a button click. The method looks like

void _buttonAction() async {

   setState(() {
     _loading = true;
   });

   // Api call takes place here

   setState(() {
      _loading = false;
   });
}

In my widget test, after the first setState I want to check whether my loading widget is displayed, but calling pump seems to progress through both the setState so when I try to check for the loading widget it fails. If I comment out the second setState then it is able to find the loading widget. How can I properly test this scenario? Here is my test code

testWidgets("Loading", (widgetTester) async {
   final Api api = MockApi();
   await widgetTester.pumpWidget(
     MyWidget()
   );


   await widgetTester.tap(find.byType(TextButton));
   await widgetTester.pump();

   expect(find.byType(Loading), findsOneWidget);
});

Thanks!

1

There are 1 answers

0
Ashikul Islam Sawan On

You can follow these steps. Add your all setState method to apiCallingMethod and check one by one. Here I'm using provider, you can use setState also. enter image description here