我使用userID如下创建数据库
当用户登录时,我可以获取userID,但是如果我使用userID从firebase数据库获取数据,则始终会出现以下错误。
请帮助解决此问题。
> NoSuchMethodError: The method '[]' was called on null. Receiver: null
> Tried calling: []("Email")
以下是我的代码:
@override
void initState() {
super.initState();
_loadCurrentUser();
shop = Shop();
databaseReference = database.reference().child("香港");
}
void _loadCurrentUser() {
FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
setState(() {
// call setState to rebuild the view
this.currentUser = user;
});
});
}
String _email() {
if (currentUser != null) {
return currentUser.email;
} else {
return "no current user";
}
}
String _userID() {
if (currentUser != null) {
debugPrint(currentUser.uid);
databaseReference.child(currentUser.uid).once().then(_updateToDo);
return currentUser.uid;
} else {
return "no current user";
}
}
@override
Widget build(BuildContext context) {
void _signOut() async {
try {
await widget.auth.signOut();
widget.onSignOut();
} catch (e) {
print(e);
}
}
return new Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
backgroundColor: Colors.black,
title: Text(_email()),
),
body:
createFireList(),
);
}
Widget createFireList() {
return Column(
children: <Widget>[
Text(_userID()),
Text(test)
],
);
}
_updateToDo(DataSnapshot map) {
if (map.value != null) {
var x = map.value["Email"];
setState(() {
test = x;
});
}
else {
debugPrint("Error");
}
}
}