【问题标题】:FirebaseDatabase: Invalid write location: /.info/connectedFirebaseDatabase:无效的写入位置:/.info/connected
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 由于答案实际上并没有给出任何线索,我的错误可能只是一个错误的复制/粘贴,在我的代码中,userStatusRef 引用了FirebaseDatabase.instance.reference().child('.info/connected');,这是一个特殊的节点,真的不是我们想要写的。切换到.child('/status/' + uid); 解决了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多