firebase - The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)' - TagMerge
2The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)'The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)'

The argument type 'AppUser? Function(User)' can't be assigned to the parameter type 'User Function(User?)'

Asked 1 years ago
1
2 answers

I wish you explained better what you are trying to do, but here is why this piece of code doesn't work:

Stream<User> get user { ... }

The values of your stream are of type User.

You are trying to map each user's values using this function:

AppUser? _userFromFirebaseUser(User user) { ... }

But that function doesn't return a User, it returns a AppUser?, very different things, you can't add a AppUser? to a Stream<User>. To fix this, change the type of your stream:

Stream<AppUser?> get user { ... }

Finally, I want you to know that user != null ? AppUser(uid: user.uid) : null; is redundant because user will never be null, user's type is User not User?, so you can actually completely get rid of nullable types:

AppUser _userFromFirebaseUser(User user) {
  return AppUser(uid: user.uid);
}

Stream<AppUser> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
}

On the off chance that user can be null, you will get an error with the above code, then you actually need to do the opposite of what I did, make everything nullable:

AppUser? _userFromFirebaseUser(User? user) {
  return user == null ? null : AppUser(uid: user.uid);
}

Stream<AppUser?> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
}

Source: link

0

I want to achieve to make a drawer with different items on it, so I am creating a separate file for the DrawerItems and the with the constructor, pass the data to the main file. But I get the following error on the onPressed function:
"The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
"The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
class DrawerItem extends StatelessWidget {
    
      final String text;
      final Function onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }
Updated code:
class DrawerItem extends StatelessWidget {
    
      final String text;
      final VoidCallback onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }
Instead of
final Function? onPressed;
use
final VoidCallback? onPressed;

Source: link

Recent Questions on firebase

    Programming Languages