Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return a exception message to the user in dart?

is it possible to return an exception message back to the user? If Yes, how may i achieve it? I want to let the user know the exception message returned whenever occured. Plus, i don’t want my app to crash at the time when the exception occurs.

To be more specific, i want to return the error message back to the user while the user is trying to login. I have used Firebase as my auth provider.

I have inlcuded my code below for a better understanding.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance.

 Future<User?> signInWithEmailAndPassword(
    String email,
    String password,
  ) async {
    try {
      final credential = await _firebaseAuth.signInWithEmailAndPassword(
          email: email, password: password);
      return _userFromFirebase(credential.user);

      throw Exception('Some Error occured. ');
    } on auth.FirebaseAuthException catch (e, _) {
      print(e);

    } catch (e) {
      print(e);
    }
  }

>Solution :

You can show an error message using ScaffoldMessenger to the user

   Future<User?> signInWithEmailAndPassword(
    BuildContext context,
    String email,
    String password,
  ) async {
    try {
      final credential = await _firebaseAuth.signInWithEmailAndPassword(
          email: email, password: password);
      return _userFromFirebase(credential.user);
      
      throw Exception('Some Error occured. ');
      
    } on auth.FirebaseAuthException catch (e, _) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text(e.toString()),
    ));
    } catch (e) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text(e.toString()),
    ));
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading