Home > Mobile >  Why this argument type can't be assigned to the parameter type?
Why this argument type can't be assigned to the parameter type?

Time:10-06

Why do I get this error in IDE ? The error is in the register function.

The argument type 'Future<UserCredential> Function({required String
email, required String password})' can't be assigned to the parameter
type 'Future<bool> Function({required String email, required String
password})'.
  @action
  Future<bool> _registerOrLogin({
    required LoginOrRegisterFunction fn,
    required String email,
    required String password,
  }) async {
    authError = null;
    isLoading = true;
    try {
      await fn(
        email: email,
        password: password,
      );
      currentUser = FirebaseAuth.instance.currentUser;
      await _loadReminders();
      return true;
    } on FirebaseAuthException catch (e) {
      currentUser = null;
      authError = AuthError.from(e);
      return false;
    } finally {
      isLoading = false;
      if (currentUser != null) {
        currentScreen = AppScreen.reminders;
      }
    }
  }

  @action
  Future<bool> register({
    required String email,
    required String password,
  }) =>
      _registerOrLogin(
        fn: FirebaseAuth.instance.createUserWithEmailAndPassword, // error is here
        email: email,
        password: password,
      );

  typedef LoginOrRegisterFunction = Future<bool> Function({
    required String email,
    required String password,
  });

CodePudding user response:

Weirdly enough, DartPad shows the correct error in the "IDE", while if I try and copy that I get your aforementioned error. Simply put, the error isn't that. The error should be that your typedef isn't returning a Future<UserCredential> as required by your context.

In other words, FirebaseAuth.instance.createUserWithEmailAndPassword does not match the type you've defined, LoginOrRegisterFunction. Which makes sense, because you wrote that a LoginOrRegisterFunction should return a Future<bool>.

Instead, FirebaseAuth.instance.createUserWithEmailAndPassword returns a Future<UserCredential>

You should either change the typedef or find another way around your problem

  • Related