我是新的 ChangeNotifier 提供程序颤振。我正在尝试从 firebase 获取用户 ID。我想从 firebase 查询当前用户的数据。但是当我运行我的应用程序时,错误发生了。
NoSuchMethodError (NoSuchMethodError: The setter 'uid=' was called on null.
Receiver: null
Tried calling: uid="AFdLhdPxWmWZ5D1HjttjQvrvKX42")
我不知道我的代码有什么问题以及如何解决这个问题。
这是我遇到问题的代码。
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final Completer<GoogleMapController> _mapController = Completer();
Customer _currentCustomer;
@override
void initState() {
AuthNotifier authNotifier =
Provider.of<AuthNotifier>(context, listen: false);
if (authNotifier.currentCustomer != null) {
_currentCustomer = authNotifier.currentCustomer;
}
super.initState();
}
@override
Widget build(BuildContext context) {
AuthNotifier authNotifier = Provider.of<AuthNotifier>(context);
return Scaffold(
appBar: AppBar(
title: Text(_currentCustomer.uid),
),
body: Container(
child: Column(children: [
Flexible(flex: 2, child: MapWidget(mapController: _mapController)),
SizedBox(height: 12),
Flexible(
flex: 2, child: StoreListWidget(mapController: _mapController)),
SizedBox(height: 12),
FloatingActionButton.extended(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
SelectMap(uid: _currentCustomer.uid)));
},
label: Text('Get Current Location'),
icon: Icon(Icons.directions_boat),
)
])));
}
}
这是 ChangeNotifier 类。
class AuthNotifier with ChangeNotifier {
User _user;
Customer _currentCustomer;
User get user => _user;
Customer get currentCustomer => _currentCustomer;
void setUser(User user) {
_user = user;
notifyListeners();
}
void setCurrentCustomer(String uid) {
_currentCustomer.uid = uid;
notifyListeners();
}
}
这是验证码。
login(Customer customer, AuthNotifier authNotifier) async {
BuildContext context;
UserCredential authResult = await FirebaseAuth.instance
.signInWithEmailAndPassword(
email: customer.email, password: customer.password)
.catchError((error) => print(error));
if (authResult != null) {
User firebaseUser = authResult.user;
if (firebaseUser != null) {
print('Login with: $firebaseUser');
authNotifier.setUser(firebaseUser);
authNotifier.setCurrentCustomer(firebaseUser.uid);
}
}
}
register(Customer customer, AuthNotifier authNotifier) async {
UserCredential authResult = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: customer.email, password: customer.password)
.catchError((error) => print(error));
if (authResult != null) {
User firebaseUser = authResult.user;
if (firebaseUser != null) {
BuildContext context;
FirebaseFirestore.instance.collection('users').doc(firebaseUser.uid).set({
'uid': firebaseUser.uid,
'userName': customer.userName,
'email': customer.email,
})
.catchError((error) => print("Failed to add user: $error"));
User currentUser = await FirebaseAuth.instance.currentUser;
authNotifier.setUser(currentUser);
authNotifier.setCurrentCustomer(firebaseUser.uid);
}
}
}
initializeCurrentUser(AuthNotifier authNotifier) async {
User firebaseUser = await FirebaseAuth.instance.currentUser;
if (firebaseUser != null) {
print(firebaseUser);
authNotifier.setUser(firebaseUser);
authNotifier.setCurrentCustomer(firebaseUser.uid);
}
}
答案 0 :(得分:0)
在您的 AuthNotifier
类中,您似乎没有初始化 _currentCustomer
。
class AuthNotifier with ChangeNotifier {
User _user;
Customer _currentCustomer; // <---- No where initialised
User get user => _user;
Customer get currentCustomer => _currentCustomer;
void setUser(User user) {
_user = user;
notifyListeners();
}
void setCurrentCustomer(String uid) {
_currentCustomer.uid = uid;
notifyListeners();
}
}
因此在 _currentCustomer.uid = uid;
中,_currentCustomer
将为空。
如果可能,我还建议您将代码迁移到 Sound Null Safety。它可以帮助您在编译时避免这些问题。