diff --git a/packages/firebase_ui_auth/lib/src/views/forgot_password_view.dart b/packages/firebase_ui_auth/lib/src/views/forgot_password_view.dart index 351a90b2..a53e30d8 100644 --- a/packages/firebase_ui_auth/lib/src/views/forgot_password_view.dart +++ b/packages/firebase_ui_auth/lib/src/views/forgot_password_view.dart @@ -73,7 +73,9 @@ class _ForgotPasswordViewState extends State { } on fba.FirebaseAuthException catch (e) { exception = e; } finally { - setState(() => isLoading = false); + if (mounted) { + setState(() => isLoading = false); + } } } diff --git a/packages/firebase_ui_auth/test/views/forgot_password_view_test.dart b/packages/firebase_ui_auth/test/views/forgot_password_view_test.dart index fb9f921b..6595b0eb 100644 --- a/packages/firebase_ui_auth/test/views/forgot_password_view_test.dart +++ b/packages/firebase_ui_auth/test/views/forgot_password_view_test.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:async'; + import 'package:firebase_auth/firebase_auth.dart' as fba; import 'package:firebase_ui_auth/firebase_ui_auth.dart'; import 'package:firebase_ui_shared/firebase_ui_shared.dart'; @@ -95,5 +97,46 @@ void main() { expect(find.byType(ErrorText), findsNothing); verify(auth.sendPasswordResetEmail(email: 'delay@email.com')).called(1); }); + + testWidgets( + "doesn't call setState if popped while sendPasswordResetEmail is pending", + (tester) async { + final completer = Completer(); + when( + auth.sendPasswordResetEmail(email: 'pending@email.com'), + ).thenAnswer((_) => completer.future); + + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (context) => TextButton( + onPressed: () => Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => + Scaffold(body: ForgotPasswordView(auth: auth)), + ), + ), + child: const Text('open'), + ), + ), + ), + ); + await tester.tap(find.text('open')); + await tester.pumpAndSettle(); + + await tester.enterText(find.byType(TextField), 'pending@email.com'); + await tester.tap(find.byType(LoadingButton)); + await tester.pump(); + + await tester.tap(find.text('Go back')); + await tester.pumpAndSettle(); + expect(find.byType(ForgotPasswordView), findsNothing); + + completer.complete(); + await tester.pump(); + + expect(tester.takeException(), isNull); + }, + ); }); }