NearHuscarl / flutter_login

Provides login screen with login/signup functionalities to help speed up development
MIT License
1.51k stars 799 forks source link

Null check operator error in _LoginCardState._submit #468

Open vargab95 opened 10 months ago

vargab95 commented 10 months ago

Describe the bug I've enable Firebase Crashlytics in my android app which detected a failed null check. My guess is that, await _submitController.reverse(); is called in an async gap and when it is called, the widget is not mounted anymore.

Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Null check operator used on a null value
       at AnimationController.stop(animation_controller.dart:778)
       at AnimationController._animateToInternal(animation_controller.dart:598)
       at AnimationController.reverse(animation_controller.dart:495)
       at _LoginCardState._submit(login_card.dart:218)

In my opinion the fix would be to check whether the context is still mounted before calling _submitController.reverse(), like the following.

diff --git a/lib/src/widgets/cards/login_card.dart b/lib/src/widgets/cards/login_card.dart
index 8db3355..e3b1ef2 100644
--- a/lib/src/widgets/cards/login_card.dart
+++ b/lib/src/widgets/cards/login_card.dart
@@ -215,7 +215,9 @@ class _LoginCardState extends State<_LoginCard> with TickerProviderStateMixin {
       }
     });

-    await _submitController.reverse();
+    if (mounted) {
+      await _submitController.reverse();
+    }

     if (!DartHelper.isNullOrEmpty(error)) {
       showErrorToast(context, messages.flushbarTitleError, error!);

To Reproduce Steps to reproduce the behavior:

  1. Go to the login page
  2. Fill the email and username with invalid credentials
  3. Click on login
  4. Push the back button of the phone to leave the screen before the result comes back
  5. Check the error log for errors

You may have to repeat it a few times as pushing the back button have to take place in exactly the right moment, however I could reproduce it several times with the described method.

Expected behavior Do not crash on the null check.

Information (please complete the following information):