首页 > 解决方案 > 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)
...

问题的根源可能是什么?

标签: firebaseflutterfirebase-realtime-databasedart

解决方案


以后我会为搜索引擎回答这个问题。

  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);
      });
    });
  }

推荐阅读