当用户登录时,我正在从appBar的firebase中检索用户名。我成功检索了它。但是,当用户未登录时。我收到错误信息,即“ uid”为null。我相信 错误是在我的initUser()中。但是我似乎无法解决它!
E / flutter(16741):[错误:flutter / lib / ui / ui_dart_state.cc(166)]未处理的异常:NoSuchMethodError:吸气剂'uid'在null上被调用。 E / flutter(16741):接收者:null E / flutter(16741):尝试调用:uid
class MainScreen extends StatefulWidget {
final bool isAdmin;
const MainScreen({Key key, this.isAdmin}) : super(key: key);
@override
_MainScreenState createState() => _MainScreenState(this.isAdmin);
}
class _MainScreenState extends State<MainScreen> {
bool isPressed = false;
bool userLoggedIn =false;
//var _userName;
// Future<FirebaseUser> getUser() {
// return _auth.currentUser();
// }
Future<String> currentUser() async {
user = await _auth.currentUser();
return user != null ? user.uid : null;
}
FirebaseUser user;
@override
void initState() {
super.initState();
initUser();
}
String userid;
initUser() async {
user = await _auth.currentUser();
userid = user.uid;
setState(() {});
}
bool isAdmin = true;
final FirebaseAuth _auth = FirebaseAuth.instance;
//final authData = snapshot.data;
_MainScreenState(this.isAdmin);
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
CurrentUser _currentUser = Provider.of<CurrentUser>(context, listen: false);
return Scaffold(
//backgroundColor: Colors.grey,
resizeToAvoidBottomPadding: false,
appBar: AppBar(
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.cyan, Colors.yellow], stops: [0.5, 1.0],
),
),
),
//title: Text('Property Host'),
centerTitle: true,
actions: <Widget>[
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
//Image.asset('assets/index.jpg', fit: BoxFit.cover,height:16,width:16),
Container(
margin: new EdgeInsets.only(left: 50),
child: Text('Property Host',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 19),)),
user!=null ? StreamBuilder(stream: Firestore.instance.collection('users').where("uid", isEqualTo: userid).snapshots(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.data == null)
return CircularProgressIndicator();
//final userDocument = snapshot.data;
//final title= snapshot.data.userocument['displayName']);
//CircularProgressIndicator();
return Expanded(
child: ListView.builder(
itemCount: snapshot.data.documents.length,
// ignore: missing_return
itemBuilder: (BuildContext context, int index) {
print(user.uid);
return user != null
? Container(
margin: EdgeInsets.only(top: 17, left: 40),
child: Text(
snapshot.data.documents.elementAt(index)['displayName']),
)
: IconButton(
icon: Icon(Icons.person),
// ignore: missing_return
onPressed: () {
Navigator.pushNamed(context, '/LoginScreen');
},
);
}
),
);
}
答案 0 :(得分:0)
当用户未登录时,await _auth.currentUser()
将返回null
。这意味着在uid
上调用user
(为空)将导致错误。
您可以将它包装在if语句中的空检查周围。
initUser() async {
user = await _auth.currentUser();
if (user != null) {
userid = user.uid;
} else {
// User is not available. Do something else
}
setState(() {});
}