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

Viewed 27

enter image description here

auth.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:lewandowski/models/bro.dart';

import '../shared/database.dart';

class AuthService{
  // method to sign in anon
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Bro? get bro => null;
  // create an object based on firebaseuser
  Bro? _userfromFirebaseUser(Bro? user){
    return User != null ? Bro(uid:user!.uid) : null;
  }

// change user stream

  Stream<Bro?> get user {
    return _auth.authStateChanges().map(_userfromFirebaseUser);
  }
  Future signAnon() async {
    try {
      UserCredential result = await _auth.signInAnonymously();
      User? user = result.user;
      return user;
    }
    catch (e) {
      print(e.toString());

      return null;
    }
  }


  Future signInWithEmailandPassword(String email, String password) async{
    try {
      UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      User? user = result.user;
      return _userfromFirebaseUser(user as Bro);
    }
    catch(e){
      print(e.toString());
    }
  }// method to sign in with email and password

  // method to sign up with email and password
  Future registerWithEmailandPassword(String email, String password) async{
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      User? user = result.user;
      // use of brewscollection
      await DatabaseService(uid: user!.uid).updateDatabase('0', 'new user', 100);
      return _userfromFirebaseUser(user as Bro);
    }
    catch(e){
      print(e.toString());
    }
  }

  //method to sign out
Future signOut() async{
    try{
      return await _auth.signOut();
    }
    catch(e){
      print(e.toString());
    }
    return null;
}

}

error on:

Stream<Bro?> get user {
        return _auth.authStateChanges().map(_userfromFirebaseUser);
says:The argument type 'Bro? Function(Bro?)' can't be assigned to the parameter type 'Bro? Function(User?)'.
Running Gradle task 'assembleDebug'...
lib/services/auth.dart:20:41: Error: The argument type 'Bro? Function(Bro?)' can't be assigned to the parameter type 'Bro? Function(User?)'.
 - 'Bro' is from 'package:lewandowski/models/bro.dart' ('lib/models/bro.dart').
 - 'User' is from 'package:firebase_auth/firebase_auth.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-3.9.0/lib/firebase_auth.dart').
    return _auth.authStateChanges().map(_userfromFirebaseUser);
                                        ^


FAILURE: Build failed with an exception.
1 Answers

The value from _auth.authStateChanges() will be User But you are expecting Bro.

Try changing the function parameter type from Bro? to User?

 Bro? _userfromFirebaseUser(User? user){
    return user != null ? Bro(uid:user!.uid) : null;
  }
Related