简体   繁体   中英

Catching errors in Flutter / Firebase Auth sign up

I'm having a bit of trouble catching errors from firebase authentication instances in flutter , I don't know what is wrong the code still throws exceptions even though i'm catching the errors , In addition , I have no idea how to identify the exception type whether its a badly formatted email , email already in use , weak password , etc.... and there is no proper documentation for such thing

I have tried this:

    FirebaseAuth.instance.createUserWithEmailAndPassword(
       email: _email, password: _password)
           .then((currentUser) => {
              //Execute            
         }).catchError((onError)=>{
              print(onError)
          });

And tried a simple try-catch block and none of them catch the exception

You need to import - import 'package:flutter/services.dart';

then try this code:

FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: _email, password: _password)
        .catchError((onError) => print(onError.message))
        .then((authResult) => {
              //Execute
            })
    FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: _email, password: _password)
              .then((currentUser) => {
                 //Execute            
             }).catchError((onError)=>{
                  //Handle error i.e display notification or toast
              });

This code actually works but the editor (Visual Studio code) itself is throwing exceptions and crashing the app , if you run the app from the device or emulator itself the issue will be solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM