I am in the process of migrating this code to the latest version of firebase and the only part I am struggling with at the moment is this code from my auth.dart file.
@override
Stream<Users> get onAuthStateChanges {
return _firebaseAuth.onAuthStateChanges.map(_userFromFirebase);
}
To migrate to the latest version, I wrote it this way:
@override
Stream<Users> get authStateChanges {
return _firebaseAuth.authStateChanges.map(_userFromFirebase);
}
I keep getting this error:
The method 'map' isn't defined for the type 'Function'.
I can't seem to find any documentation on how to write it properly. Would someone happen to know how to fix this?
Thanks in advance for your help
PS
In case you're wondering why I'm migrating this code, my Emulator keeps complaining that the Firebase Core plugin is missing (I guess due to the Google-services.json file) and none of the authentication buttons work. Upon clicking I get this "return firebase.google.cms" error". The Simulator was working fine until I did a recent update on Xcode and now it's giving me this weird error code and will not complete the flutter run. So I figured if I get the latest version of firebase and update the code, accordingly, I can use my Simulator and/or Emulator again. Then I encountered the above problem with the authStateChanges.map code and here we are.
Here is my auth.dart file in its entirety for your reference:
class Users {
final String uid;
Users({@required this.uid});
}
abstract class AuthBase {
Stream<Users> get authStateChanges;
Future<Users> currentUser();
Future<Users> signInAnonymously();
Future<Users> signInWithEmailAndPassword(String email, String password);
Future<Users> createUserWithEmailAndPassword(String email, String password);
Future<Users> signInWithGoogle();
Future<void> signOut();
}
class Auth implements AuthBase {
final _firebaseAuth = FirebaseAuth.instance;
//To avoid confusion due to updates, "Users" come from the class Users and "User" replaces the deprecated "FirebaseUser".
Users _userFromFirebase(User user) {
if (user == null) {
return null;
}
return Users(uid: user.uid);
}
@override
Stream<Users> get authStateChanges {
return _firebaseAuth.authStateChanges.map(_userFromFirebase);
}
@override
Future<Users> currentUser() async {
final user = _firebaseAuth.currentUser;
return _userFromFirebase(user);
}
@override
Future<Users> signInAnonymously() async {
final authResult = await _firebaseAuth.signInAnonymously();
return _userFromFirebase(authResult.user);
}
@override
Future<Users> signInWithEmailAndPassword(String email, String password) async {
final authResult = await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
return _userFromFirebase(authResult.user);
}
@override
Future<Users> createUserWithEmailAndPassword(
String email, String password) async {
final authResult = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
return _userFromFirebase(authResult.user);
}
@override
Future<Users> signInWithGoogle() async {
final googleSignIn = GoogleSignIn();
final googleAccount = await googleSignIn.signIn();
if (googleAccount != null) {
final googleAuth = await googleAccount.authentication;
if (googleAuth.accessToken != null && googleAuth.idToken != null) {
final authResult = await _firebaseAuth.signInWithCredential(
GoogleAuthProvider.credential(
idToken: googleAuth.idToken, accessToken: googleAuth.accessToken),
);
return _userFromFirebase(authResult.user);
} else {
throw PlatformException(
code: 'ERROR_MISSING_GOOGLE_AUTH_TOKEN',
message: 'Missing Google Auth Token');
}
} else {
throw PlatformException(
code: 'ERROR_ABORTED_BY_USER', message: 'Sign in aborted by user');
}
}
@override
Future<void> signOut() async {
final googleSignin = GoogleSignIn();
await googleSignin.signOut();
await _firebaseAuth.signOut();
}
}
Here are the dependencies I'm using:
dependencies:
flutter:
sdk: flutter
font_awesome_flutter: ^8.8.1
image_picker: ^0.6.7+6
provider: ^4.3.2+1
path_provider: ^1.6.14
path: ^1.7.0
firebase_core: ^0.5.0
cloud_firestore: ^0.14.1+2
google_sign_in: ^4.5.3
firebase_auth: ^0.18.1+1
firebase_storage: ^4.0.1
image: ^2.1.18
animator: ^2.0.1
geolocator: ^5.3.2+2
uuid: ^2.2.2
cached_network_image: ^2.3.2+1
bubble: ^1.1.9+1
flutter_cupertino_date_picker: ^1.0.26+2