firebase - FirebaseDatabase:写入位置无效:/.info/connected
问题描述
https://firebase.google.com/docs/firestore/solutions/presence的 Dart 实现
class RealtimeDatabase {
final FirebaseDatabase db = FirebaseDatabase.instance;
final AuthService authService = locator<AuthService>();
Future<void> connect() async {
// Fetch current user's ID from authentication service
User user = authService.currentUser;
String uid = user.uid;
// Create reference to this user's specific status node
// This is where we will store data about being online/offline
var userStatusRef = db.reference().child('/status/' + uid);
// We'll create two constants which we will write to the
// Realtime database when this device is offline or online
var isOfflineForDatabase = {
'state': 'offline',
'last_changed': ServerValue.timestamp,
};
var isOnlineForDatabase = {
'state': 'online',
'last_changed': ServerValue.timestamp,
};
// db.goOnline();
// Create a ref to '.info/connected' path in Realtime db
// This path returns true when connected and false when disconnected
db
.reference()
.child('.info/connected')
.onDisconnect()
.set(isOfflineForDatabase)
.then((_) => userStatusRef.set(isOnlineForDatabase));
}
}
我们收到以下错误:无效的写入位置:/.info/connected
E/MethodChannel#plugins.flutter.io/firebase_database(17220): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_database(17220): com.google.firebase.database.DatabaseException: Invalid write location: /.info/connected
E/MethodChannel#plugins.flutter.io/firebase_database(17220): at com.google.firebase.database.core.utilities.Validation.validateWritablePath(com.google.firebase:firebase-database@@17.0.0:127)
E/MethodChannel#plugins.flutter.io/firebase_database(17220): at com.google.firebase.database.DatabaseReference.onDisconnect(com.google.firebase:firebase-database@@17.0.0:475)
E/MethodChannel#plugins.flutter.io/firebase_database(17220): at io.flutter.plugins.firebase.database.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:466)
...
问题的根源可能是什么?
解决方案
以后我会为搜索引擎回答这个问题。
Future<void> connect() async {
// Fetch current user's ID from authentication service
User user = authService.currentUser;
String uid = user.uid;
// Create reference to this user's specific status node
// This is where we will store data about being online/offline
var userStatusRef = db.reference().child('/status/' + uid);
// We'll create two constants which we will write to the
// Realtime database when this device is offline or online
var isOfflineForDatabase = {
'state': 'offline',
'last_changed': ServerValue.timestamp,
};
var isOnlineForDatabase = {
'state': 'online',
'last_changed': ServerValue.timestamp,
};
// This is the correct implementation
db.reference().child('.info/connected').onValue.listen((data) {
if (data.snapshot.value == false) {
return;
}
userStatusRef.onDisconnect().set(isOfflineForDatabase).then((_) {
userStatusRef.set(isOnlineForDatabase);
});
});
}
推荐阅读
- javascript - 时刻/时刻时区中是否有任何内置函数可以将时区名称转换为“人类可读格式”?
- intellij-idea - 当我尝试设置 Google Cloud Source Repositories 时 Intellij IDEA 出错
- javascript - 在 react-native 组件中通过名称导入多个 SVG
- java - 对多个匹配建议的 Aspectj 异常处理
- django-rest-framework - 如何在 django rest 框架中处理多个 slug
- javascript - 使用 GET 下载使用 POST 创建的文件
- oracle - 如何将图像作为 BLOB 插入 Oracle 数据库
- javascript - 在 HTML 表格中隐藏部分单元格
- javascript - ThreeJS - 未捕获的 TypeError:b[c].call 不是函数 - Orbit Controls
- html - 滚动时保持在顶部的凹头