2017-05-25 70 views
2

通常,您可以在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); 
    }, 
    ), 

回答

0

在顫,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進行更多的查詢。 –

0

我覺得這是@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()' –