title: Flutter widget test exception not being handled properly
body: <p>I do not believe this is a duplicate of <a href="https://stackoverflow.com/questions/54262024/flutter-testing-for-exceptions-in-widget-tests">this SO issue</a> because the stated solution does not work in my case.</p>
  <p>I am not able to properly catch / verify an Exception in my widgetTest and I am not sure why. Instead of handling the Exception, the test actually just fails on the Exception that I am trying to test.</p>
  <p>Here is my code:</p>
  <pre><code>child: ElevatedButton(
    onPressed: () {
  try {
    bool loggedIn = await widget.authHandler.login(email, password);
  } on InvalidStateException catch (e) {
    setState(() {
      errorMessage = 'Error Logging In. Please Try Again';
      loginError = true;
    });
  }
  })
  </code></pre>
  <p>Here is my test:</p>
  <pre><code>testWidgets('Testing Login button Failure - Exception Thrown',
          (tester) async {
        final amplifyAuthMock = MockAmplifyAuth();
        when(amplifyAuthMock.login('test@test.com', 'password!'))
            .thenThrow(InvalidStateException);
        await tester.pumpWidget(createLoginForm(amplifyAuthMock));
        await inputDummyLoginText(tester);
        await tester.tap(find.byType(ElevatedButton));
        expect(tester.takeException(), isInstanceOf&lt;InvalidStateException&gt;());
        await tester.pumpAndSettle(const Duration(seconds: 1));
        expect(find.text('Error Logging In. Please Try Again'), findsOneWidget);
  });
  </code></pre>
  <p>When the test is run, this is the error I get:</p>
  <pre><code>The following TestFailure object was thrown running a test (but after the test had completed):
    Expected: &lt;Instance of 'InvalidStateException'&gt;
    Actual: &lt;null&gt;
     Which: is not an instance of 'InvalidStateException'
  </code></pre>
  <p>and if I change the test to be</p>
  <pre><code>expect(() async =&gt; await tester.tap(find.byType(SkillTreeElevatedButton)), throwsA(isA&lt;InvalidStateException&gt;()));
  </code></pre>
  <p>Then I still get</p>
  <pre><code>The following TestFailure object was thrown running a test (but after the test had completed):
    Expected: throws &lt;Instance of 'InvalidStateException'&gt;
    Actual: &lt;Closure: () =&gt; Future&lt;void&gt;&gt;
     Which: returned a Future that emitted &lt;null&gt;
  </code></pre>
  <p>I was thinking that since the function I am mocking returns the exception, and that is caught in the onPressed widget code, that I wouldn't have any problems. Best case scenario, I can catch the occurance of the exception and verify it happened, and worst case I could at least just see that my error text was added to the page... but I'm just getting this error. Am I going about handling errors wrong in Flutter?</p>