【发布时间】:2020-08-30 15:26:25
【问题描述】:
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)
...
问题的根源是什么?
【问题讨论】:
标签: firebase flutter firebase-realtime-database dart