
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;
}
}以下错误:
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.发布于 2022-09-21 11:09:07
来自_auth.authStateChanges()的值将是User,但您期待的是Bro。
尝试将函数参数类型从Bro?更改为User?
Bro? _userfromFirebaseUser(User? user){
return user != null ? Bro(uid:user!.uid) : null;
}https://stackoverflow.com/questions/73799687
复制相似问题