【发布时间】:2021-11-03 22:13:35
【问题描述】:
当我尝试在我的 appBar 中显示用户的状态时,我遇到了这个问题:
appBar: AppBar(
title: StreamBuilder<DocumentSnapshot>(
stream: _firestore.collection('users').doc(userMap['uid']).snapshots(),
builder: (context, snapshot){
if (snapshot.data != null){
return Container(
child: Column(
children: [
Text(userMap['name']),
Text(
snapshot.data!['status'],
style: TextStyle(fontSize: 14),
)
],
),
);
}else{
return Container();
}
}
),
backgroundColor: Colors.white,
leading: IconButton(
icon:Icon(Icons.arrow_back_rounded, color: Colors.black,), onPressed: () {
Navigator.of(context)
. pushReplacement(MaterialPageRoute(builder: (_) => chatpage()));},
),
),
在firebase中查看用户状态的所有过程我认为问题出在第一部分,但我展示了其余代码。
class _chatpageState extends State<chatpage> with WidgetsBindingObserver{
Map<String,dynamic>? userMap;
bool isLoading = false;
final FirebaseAuth _auth = FirebaseAuth.instance;
final TextEditingController _search = TextEditingController();
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
@override
void initState(){
super.initState();
WidgetsBinding.instance!.addObserver(this);
setStatus("Online");
}
void setStatus(String status)async{
await _firestore.collection('users').doc(_auth.currentUser!.uid).update({
"status": status
});
}
@override
void didChangeAppLifecycleState(AppLifecycleState state){
if(state == AppLifecycleState.resumed){
setStatus("Online");
//online
}else{
setStatus("Offline");
//offline
}
}
我的方法有一小部分:
await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
"name": name,
"email": email,
"status": "Unavalible",
"uid": _auth.currentUser!.uid,
});
如果 somoene 能帮上忙,我会非常感激 :)
【问题讨论】:
标签: android firebase flutter google-cloud-firestore