如何在用戶在線和用戶離線/關閉應用程式時創建在線狀態欄,如何使用 firebase 放置類似于“12 分鐘前在線”的內容。我不知道它如何與顫振和火力一起作業。
現在它使用此代碼作業
final FirebaseAuth _auth = FirebaseAuth.instance;
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) {
// online
setStatus('Online');
} else if (state == AppLifecycleState.paused ||
state == AppLifecycleState.inactive) {
// offline
setStatus('Offline');
}
}
uj5u.com熱心網友回復:
您可以使用
const oneMin = Duration(minutes:1);
Timer.periodic(oneMin, (Timer t){
//your function to save current time in firebase
//Can also check last seen of other users here
})
但是當您檢查其他用戶的最后一次看到時間時,如果 lastseen 在一分鐘之前,則用戶離線,否則用戶在線
uj5u.com熱心網友回復:
可以WidgetsBindingObserver用來管理用戶的在線/離線狀態
添加觀察者
class _HomeState extends State<Home> with WidgetsBindingObserver {
...
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
移除觀察者
@override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
處理狀態
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed) {
updateStatus(true);
} else if (state == AppLifecycleState.paused ||
state == AppLifecycleState.inactive) {
updateStatus(false);
}
}
如何使用 firebase 放置類似于“12 分鐘前在線”的內容。
為此,您可以在用戶離線時將當前時間戳存盤在 Firebase 中。并以以前的格式顯示時間/日期,您可以使用timeago
uj5u.com熱心網友回復:
您所描述的內容被稱為狀態系統,并且在 Firebase 檔案中對其實時資料庫的離線功能進行了很好的描述,特別是關于管理狀態的部分和狀態應用程式的完整示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446132.html
