flutter firebase_database连接两个节点 [英] flutter firebase_database joining two nodes

查看:35
本文介绍了flutter firebase_database连接两个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,您可以在JavaScript中使用地图功能来更新返回的数据.我似乎找不到在Dart Streams中允许这种操作的方法.

Typically you can use a map function in javascript to update the data coming back. I can not seem to find a method to allow for this in Dart Streams.

示例 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尝试此操作.

I was trying this using the flutter firebase_database.

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

但是从来没有调用map函数,所以当从Firebase获取数据时,我无法加入任何其他数据.

However the map function never gets called so I cannot join any other data when it gets fetched from Firebase.

此外,我要使用的结尾是FirebaseAnimatedList,所以如果我不做地图,如何传递查询?

Also the end of this what I am trying to use is a FirebaseAnimatedList, so how can I pass a Query if I don't do a map??

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

推荐答案

在Flutter中, DatabaseReference.onValue Stream ,因此您必须调用 Stream.listen 并取消您的<完成收听后,请选择code> StreamSubscription .如果只想获取一个值事件,则应调用 once()以获取表示快照值的 Future .完成后,您将获得快照.

In Flutter, DatabaseReference.onValue is a Stream, so you have to call Stream.listen on it and cancel your StreamSubscription when you're done listening. If you just want to get one value event, you should instead call once() to get a Future representing the snapshot value. When it completes, you'll have your snapshot.

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

如果您在 async 函数中执行此操作,甚至会更加简单,因为您可以调用 await 来获取 once()的结果>:

This can be even simpler if you do it in an async function because you can just call await to get the result of once():

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

这篇关于flutter firebase_database连接两个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆