扑火firebase_database加入两个节点

扑火firebase_database加入两个节点

问题描述:

通常,您可以在JavaScript中使用地图功能来更新回来的数据。我似乎无法找到在Dart Streams中允许使用的方法。扑火firebase_database加入两个节点

https://firebase.googleblog.com/2013/10/queries-part-1-common-sql-queries.html

var fb = new Firebase("https://examples-sql-queries.firebaseio.com/"); 
fb.child('user/123').once('value', function(userSnap) { 
    fb.child('media/123').once('value', function(mediaSnap) { 
     // extend function: https://gist.github.com/katowulf/6598238 
     console.log(extend({}, userSnap.val(), mediaSnap.val())); 
    }); 
}); 

我这个用扑firebase_database尝试。

var _fireFollower =FirebaseDatabase.instance.reference() 
     .child('followers/' + auth.currentUser.uid); 
    _fireFollower.onValue.map((snap){ 
    print(snap.snapshot.key); 
    }); 

但是map函数永远不会被调用,所以当它从Firebase获取时,我无法加入任何其他数据。

此外,我想使用的是一个FirebaseAnimatedList,所以我怎么能通过一个查询,如果我不做地图?

new FirebaseAnimatedList(
query: _fireFollower, 
    sort: (a, b) => b.key.compareTo(a.key), 
    padding: new EdgeInsets.all(8.0), 
    reverse: false, 
    itemBuilder: (_, DataSnapshot snapshot, 
    Animation<double> animation) { 
    return new Activity(
    snapshot: snapshot, animation: animation); 
    }, 
    ), 

在颤,DatabaseReference.onValueStream,所以你要调用它的Stream.listen并取消StreamSubscription当你完成了倾听。如果您只想获取一个值事件,则应该调用once()以获取代表快照值的Future。完成后,您将获得快照。

Future.wait([fb.child('media/123').once(), fb.child('user/123').once()]) 
    .then((snapshots) => print("Snapshots are ${snapshots[0]} ${snapshots[1]}")); 

如果你在一个async功能做这可能是更简单,因为你可以叫await得到的once()结果:

print("Snapshots are ${await ref1.once()} ${await ref2.once()}"); 
+0

飞镖和颤振都是新的给我,但我觉得有什么不对的插件firebase_database。 我在github中添加了更多插件[https://github.com/flutter/flutter/issues/10321](https://github.com/flutter/flutter/issues/10321) 它在我看来,我应该可以通过使用map函数来操作Stream'onValue'。 – ajonp

+0

已更新的问题如何与FirebaseAnimatedList一起使用。 @ collin-jackson – ajonp

+1

您可以使用FutureBuilder读取您想要的数据,并确保它在构建列表时可用。如果您不需要它在数据更改时自动更新,请考虑使用常规ListView。或者你可以使用FutureBuilder进行更多的查询。 –

我觉得这是@collin建议的解决方案-jackson,这里是我的代码...

return new Column(
        mainAxisAlignment: MainAxisAlignment.start, 
        children: <Widget>[ 
        new Flexible(
         child: new FirebaseAnimatedList(
         query: FirebaseDatabase.instance 
           .reference() 
           .child('followers/${auth.currentUser.uid}'), 
         sort: (a, b) => b.key.compareTo(a.key), 
         padding: new EdgeInsets.all(8.0), 
         reverse: false, 
         itemBuilder: (_, DataSnapshot followerSnap, 
          Animation<double> animation) { 
          return new FutureBuilder<DataSnapshot>(
          future: FirebaseDatabase.instance 
           .reference() 
           .child('users/${followerSnap.key}') 
           .once(), 
          builder: (BuildContext context, 
           AsyncSnapshot<DataSnapshot> userSnap) { 
           switch (userSnap.connectionState) { 
           case ConnectionState.none: 
            return new Text('Loading...'); 
           case ConnectionState.waiting: 
            return new Text('Awaiting result...'); 
           default: 
            if (userSnap.hasError) 
            return new Text(
             'Error: ${userSnap.error}'); 
            else 
            return new User(snapshot: userSnap.data, animation: animation); 
           } 
          }, 
         ); 
         }, 
        ), 
        ), 
        ]); 

类用户

@override 
class User extends StatelessWidget { 
    User({this.snapshot, this.animation}); 
    final DataSnapshot snapshot; 
    final Animation animation; 

    Widget build(BuildContext context) { 

    return new SizeTransition(
     sizeFactor: new CurvedAnimation(parent: animation, curve: Curves.easeOut), 
     axisAlignment: 0.0, 
     child: new Container(
     margin: const EdgeInsets.symmetric(vertical: 10.0), 
     child: new Row(
      crossAxisAlignment: CrossAxisAlignment.start, 
      children: <Widget>[ 
      new Container(
       margin: const EdgeInsets.only(right: 16.0), 
       child: snapshot.value['photoURl'] == null 
        ? new CircleAvatar() 
        : new CircleAvatar(
       backgroundImage: new NetworkImage(snapshot.value['photoURl']), 
      ), 
      ), 
      new Column(
       crossAxisAlignment: CrossAxisAlignment.start, 
       children: <Widget>[ 
       new Text(snapshot.value['displayName'], 
        style: Theme.of(context).textTheme.subhead), 
       ], 
      ), 
      ], 
     ), 
    ), 
    ); 
    } 
} 

Followers, referencing Users.

+0

这是一个很好的答案,但我会使用'StreamBuilder'和'onValue'来代替'FutureBuilder'和'once()' –