Home > Enterprise >  Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the fut
Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the fut

Time:05-19

I am trying to write a dart code program which can register users and sign in users, upon error print its description which i will eventually use in a Toast but it given an error on catchError

Future<void> signIn(String email, String password) async {
    if (_form.currentState!.validate()) {
      print(email);
      await _auth
          .signInWithEmailAndPassword(email: email, password: password)
          .then((uid) => {
        Fluttertoast.showToast(msg: "Login Successfully"),
        Navigator.of(context)
            .pushReplacementNamed(UploadScreen.routeName),
      })
          .catchError((e) {
        Fluttertoast.showToast(msg: 'Incorrect Email or Password.',
            toastLength: Toast.LENGTH_LONG
        );
      });
    }
  }

E/flutter ( 7093): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type E/flutter ( 7093): #0 _FutureListener.handleError (dart:async/future_impl.dart:193:7) E/flutter ( 7093): #1 Future._propagateToListeners.handleError (dart:async/future_impl.dart:778:47) E/flutter ( 7093): #2 Future._propagateToListeners (dart:async/future_impl.dart:799:13) E/flutter ( 7093): #3 Future._completeError (dart:async/future_impl.dart:609:5) E/flutter ( 7093): #4 _completeOnAsyncError (dart:async-patch/async_patch.dart:272:13) E/flutter ( 7093): #5 FirebaseAuth.signInWithEmailAndPassword (package:firebase_auth/src/firebase_auth.dart) E/flutter ( 7093): E/flutter ( 7093):

CodePudding user response:

Try:

Future<void> signIn(String email, String password) async {
    try{
        final User user = (await _auth.signInWithEmailAndPassword(
          email: email,
          password: password,
        )).user;
        Fluttertoast.showToast(msg: "Login Successfully"),
        Navigator.of(context)
          .pushReplacementNamed(UploadScreen.routeName),
      } catch (e) {
        Fluttertoast.showToast(msg: 'Incorrect Email or Password.',
            toastLength: Toast.LENGTH_LONG
        );
      }
  }

CodePudding user response:

You can use one rror function

Future<void> signIn(String email, String password) async {
    if (_form.currentState!.validate()) {
      print(email);
      await _auth
          .signInWithEmailAndPassword(email: email, password: password)
          .then((uid) => {
        Fluttertoast.showToast(msg: "Login Successfully"),
        Navigator.of(context)
            .pushReplacementNamed(UploadScreen.routeName),
      }).onError((e, s) {
        Fluttertoast.showToast(msg: 'Incorrect Email or Password.',
            toastLength: Toast.LENGTH_LONG
        );
      });
    }
  }
  • Related