title: Trouble with Dart Futures in Flutter : Failed assertion: line 146: '<optimized out>': is not true
body: <p>I'm building a user authentication module for my app and I am running into trouble with some asynchronous code.</p>
  
  <p>Firstly, here is the error that is thrown:</p>
  
  <blockquote>
    <p>E/flutter (17162): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
    E/flutter (17162): 'dart:async/future_impl.dart': Failed assertion: line 146: 'optimized out': is not true.
    E/flutter (17162): #0      _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
    E/flutter (17162): #1      _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
    E/flutter (17162): #2      _FutureListener.handleError (dart:async/future_impl.dart:146:14)
    E/flutter (17162): #3      Future._propagateToListeners.handleError (dart:async/future_impl.dart:654:47)
    E/flutter (17162): #4      Future._propagateToListeners (dart:async/future_impl.dart:675:24)
    E/flutter (17162): #5      Future._completeError (dart:async/future_impl.dart:494:5)
    E/flutter (17162): #6      _SyncCompleter._completeError (dart:async/future_impl.dart:55:12)
    E/flutter (17162): #7      _Completer.completeError (dart:async/future_impl.dart:27:5)
    E/flutter (17162): #8      _AsyncAwaitCompleter.completeError (dart:async/runtime/libasync_patch.dart:40:18)
    E/flutter (17162): #9      FirebaseAuth.signInWithEmailAndPassword (package:firebase_auth/firebase_auth.dart)
    E/flutter (17162): 
    E/flutter (17162): #10     Session.login. (package:mood_map/utilities/session.dart:31:24)
    E/flutter (17162): #11     _RootZone.runUnary (dart:async/zone.dart:1379:54)
    E/flutter (17162): #12     _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
    E/flutter (17162): #13     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
    E/flutter (17162): #14     Future._propagateToListeners (dart:async/future_impl.dart:671:32)
    E/flutter (17162): #15     Future._complete (dart:async/future_impl.dart:476:7)
    E/flutter (17162): #16     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
    E/flutter (17162): #17     _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
    E/flutter (17162): #18     _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:295:13)
    E/flutter (17162): #19     Session._checkUserAlreadyExists (package:mood_map/utilities/session.dart)
    E/flutter (17162): 
    E/flutter (17162): #20     Session.login (package:mood_map/utilities/session.dart:27:11)</p>
  </blockquote>
  
  <p>And here are the functions that are involved:</p>
  
  <pre><code>static final FirebaseAuth _authenticator = FirebaseAuth.instance;
  
  static void login(BuildContext context, String email, String password) async {
  
  email = email.trim();
  password = password.trim();
  
  //Check if the user already exists
  await _checkUserAlreadyExists(email).then((exists) {
  
    if(exists) {
  
      _authenticator.signInWithEmailAndPassword(email: email, password: password)
          .then((FirebaseUser user) { _loginSuccess(); })
          .catchError((Error e) { _loginFailure(context); });
  
    } else {
  
      Utilities.showMessageDialog(context, "That user doesn't exist. Please create an account below.");
  
    }
  
  });
  
  } 
  
  ----------------------------------------------------------------------
  
  static Future createUserAccount(BuildContext context, email, String password) async {
  
  //Check if the user already exists
  await _checkUserAlreadyExists(email).then((exists) {
  
    if(exists) {
  
      Utilities.showMessageDialog(context, "That user already exists. Please login or select another account.");
      AppNavigator.navigateToLoginScreen();
  
    } else {
  
      _authenticator.createUserWithEmailAndPassword(email: email, password: password)
          .then((FirebaseUser user) { _createUserSuccess(); })
          .catchError((Error e) { _createUserFailure(context); });
  
    }
  
  });
  
  }
  </code></pre>
  
  <p>In short, the call to _authenticator.signonWithEmailAndPassword() is failing. I know that the _authenticator instance is working with other functions so I know it isnt a problem with Firebase itself.</p>
  
  <p>I am worried that I am doing something incorrectly by calling another asynchronous function, _authenticator.signonWithEmailAndPassword() from within another asynchronous function, _checkIfUserAlreadyExists(). It seems that this should be okay to do from within a .then() block from what I've read but the error message seems pretty insistent that it is something to do with the setup of the asynchronous nature of the function calls.</p>
  
  <p>Thoughts?</p>