尝试将Flutter应用程序与Firebase连接时出现此错误,我不知道为什么会出现。
不能将参数类型'Future
Function(FirebaseUser)'分配给参数类型'void Function(User)'
错误在_onStateChanged
的这一行出现。全行为_auth.onAuthStateChanged.listen(_onStateChanged);
这是我的auth.dart代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:sokoni/src/helpers/user.dart';
import 'package:sokoni/src/models/user.dart';
enum Status {Uninitialized, Unauthenticated, Authenticating, Authenticated}
class AuthProvider with ChangeNotifier{
// we're using the methods of the ChangeNotifier class in the AuthProvider class
FirebaseAuth _auth;
// ignore: deprecated_member_use
FirebaseUser _user;
Status _status = Status.Uninitialized;// initial status
UserServices _userServices = UserServices();
UserModel _userModel;
//getters => we can access the values of a class but we cannot modify the values
Status get status => _status;
UserModel get userModel => _userModel;
// ignore: deprecated_member_use
FirebaseUser get user => _user;
// ignore: non_constant_identifier_names
final FormKey = GlobalKey<FormState>();
TextEditingController email = TextEditingController();
TextEditingController name = TextEditingController();
TextEditingController password = TextEditingController();
AuthProvider.initialize(): _auth = FirebaseAuth.instance{
//this listens to the user authentication state the we will do something about once a change is caught
// ignore: deprecated_member_use
_auth.onAuthStateChanged.listen(_onStateChanged);
}
Future<bool> signIn() async{
try{
_status = Status.Authenticating;
notifyListeners();
await _auth.signInWithEmailAndPassword(email: email.text, password: password.text);
return true;
}catch(e){
return _onError(e.toString());
}
}
Future signOut(){
_auth.signOut();
_status = Status.Unauthenticated;
notifyListeners();
return Future.delayed(Duration.zero);
}
Future<bool> signUp() async{
try{
_status = Status.Authenticating;
notifyListeners();
await _auth.createUserWithEmailAndPassword(email: email.text, password: password.text).then((user) {
Map<String, dynamic> values = {
"name": name.text,
"email": email.text,
"id": user.user.uid,
};
_userServices.createUser(values);
});
return true;
}catch(e){
return _onError(e.toString());
}
}
// ignore: deprecated_member_use
Future<void> _onStateChanged(FirebaseUser firebaseUser) async{
if(firebaseUser == null){
_status = Status.Uninitialized;
}else {
_user = firebaseUser;
_status = Status.Authenticated;
_userModel = await _userServices.getUserById(firebaseUser.uid);
}
notifyListeners();
}
// my gen methods
bool _onError(String error){
_status = Status.Unauthenticated;
notifyListeners();
print("We got an error: $error");
return false;
}
void cleanControllers (){
email.text = " ";
password.text = " ";
name.text = " ";
}
}
答案 0 :(得分:0)
我认为您需要的是
_auth.onAuthStateChanged.listen((User user) async{
await _onStateChanged(user);
}
);
如另一个答案所示,将FirebaseUser
更改为User
答案 1 :(得分:0)
FirebaseUser
,并替换为User
,
您应该更改您的方法
Future<void> _onStateChanged(FirebaseUser firebaseUser) async{
if(firebaseUser == null){
_status = Status.Uninitialized;
}else {
_user = firebaseUser;
_status = Status.Authenticated;
_userModel = await _userServices.getUserById(firebaseUser.uid);
}
notifyListeners();
}
到
Future<void> _onStateChanged(User user) async{
if(user== null){
_status = Status.Uninitialized;
}else {
_user = user;
_status = Status.Authenticated;
_userModel = await _userServices.getUserById(user.uid);
}
notifyListeners();
}
您可以在docs那里查看新功能