title: With Flutter, how can I display Firebase Auth error messages caught within an AuthService class within a separate log in page widget?
body: <p>I have a Registration Page with my sign up form and then an AuthService class I call to sign up the user, returning a mapped custom User class. I can check if the result of the function call is not null and therefore navigate my user to the home page, but I can't work out how to setState or similar to actually show the user the Firebase Auth messages in my Registration page, as the try/catch block is within my auth service class. </p>
  
  <p>This is my abbreviated Registration screen widget:</p>
  
  <pre><code>class RegistrationScreen extends StatefulWidget {
    @override
    _RegistrationScreenState createState() =&gt; _RegistrationScreenState();
  }
  
  class _RegistrationScreenState extends State&lt;RegistrationScreen&gt; {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        //abbreviated...
                  RoundedButton(
                    onPressed: () async {
                      if (_formKey.currentState.validate()) {
                        dynamic result = await _auth.registerWithEmailAndPassword(email, password, displayName);
                       if (result != null) {
                         Navigator.pushNamedAndRemoveUntil(context, Home.id, (_) =&gt; false);
                        } 
                       }
                    }
                  }
    }
  </code></pre>
  
  <p>The registerWithEmailAndPassword is within an imported AuthService class auth.dart:</p>
  
  <pre><code>class AuthService {
    final FirebaseAuth _auth = FirebaseAuth.instance;
    final _firestore = Firestore.instance;
    //create User object
    User _userFromFirebaseUser(FirebaseUser user) {
      return user != null ? User(uid: user.uid, displayName: user.displayName) : null;
    }
  
    //auth change user stream
    Stream&lt;User&gt; get user {
      return _auth.onAuthStateChanged
      .map(_userFromFirebaseUser);
    }
  
    // register
    Future registerWithEmailAndPassword(String email, String password, String displayName) async {
                        try { 
                        AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
                        FirebaseUser user = result.user;
  
                        return _userFromFirebaseUser(user);
                      } catch(e) {
                        print(e);
                      } 
                        } 
    }
  </code></pre>
  
  <p>If I then test this with a badly formatted email, I correctly print to the console: </p>
  
  <pre><code>flutter: PlatformException(ERROR_INVALID_EMAIL, The email address is badly formatted., null)
  </code></pre>
  
  <p>However how can I use that PlatformException to setState or similar within my registration screen to show the e.message to the user?</p>
  
  <p>Thanks.</p>