Home > OS >  Undefined name 'Utils' error in catch exception
Undefined name 'Utils' error in catch exception

Time:06-20

I am working on Flutter Firebase send verification email page but I am getting the Undefined name Utils error after placing a catch error exception. Below is the snippet of the code:

Future sendVerificationEmail() async {
    try {
      final user = FirebaseAuth.instance.currentUser!;
      await user.sendEmailVerification();

      setState(() => canResendEmail =` false);
      await Future.delayed(const Duration(seconds: 5));
      setState(() => canResendEmail = true);
    } catch (e) {
        Utils.showSnackBar(e.toString());
    }
  }

CodePudding user response:

I could be wrong, but I think whoever wrote that code also wrote a Utils class with that function as well. It's not part of any standard library. And the exact implementation we can't know. Obviously it's some helper method to show a Snackbar. It usually requires a BuildContext but you could check out How to show snackBar without Scaffold to see alternatives.

And a quick google makes me think that you simple copied that code from https://serveanswer.com/issue/how-to-use-google-login-in-flutter-and-bypass-emailverification since it has the exact same method. I apologize if that's not the case. My advice is to not simply copy pasting code without understanding what's happening. Especially code that is not part of a guide.

In any case, the code that doesn't work shows a message to the user through a Snackbar in case an exception happens. Which isn't really user friendly message so it might be not that great to write it like that anyway.

CodePudding user response:

I think you don't added import 'package:basic_utils/basic_utils.dart';

or

make sure u added this in pubspec.yaml file

  dependencies:
    basic_utils: ^4.2.2
  • Related