如何为FirebaseAnimated List迭代Flutter AsyncSnapshot <datasnapshot>?

时间:2017-10-07 09:33:11

标签: firebase-realtime-database iteration snapshot flutter

我试图遍历我的Firebase树以填充FirebaseAnimatedList。我需要使用密钥以及所有提供的值。我假设迭代是异步构建到窗口小部件中的,这就是它不能像JSON迭代那样工作的原因。这是我的代码......

child: new FirebaseAnimatedList(
                      query: fb.child('NumberOnes').orderByChild('Value'),
                      padding: new EdgeInsets.all(8.0),
                      reverse: false,
                      itemBuilder: (_, DataSnapshot followerSnap,
                          Animation<double> animation, int Index) {
                        return new FutureBuilder<DataSnapshot>(
                            future: fb
                                .child('NumberOnes')
                                .orderByChild('Value')
                                .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
                                    print(userSnap.data.value.toString());
                                    return new Container(
                                      child: new Text(
                                          userSnap.data.value.toString()),
                                    );
                              }
                            });
                      })

以下是print(userSnap.data.value.toString()); ...

的日志
{aZb6KxxIuWA: {Value: 1, vidImage: https://i.ytimg.com/vi/aZb6KxxIuWA/mqdefault.jpg, vidId: aZb6KxxIuWA, vidTitle: 10 Embarrassing K-POP Star Fails And Accidents}, 4408NthSJis: {Value: 1, vidImage: https://i.ytimg.com/vi/4408NthSJis/mqdefault.jpg, vidId: 4408NthSJis, vidTitle: [TOP 100] MOST VIEWED K-POP MUSIC VIDEOS • APRIL 2017}, 7n5ieHnu90w: {Value: 1, vidImage: https://i.ytimg.com/vi/7n5ieHnu90w/mqdefault.jpg, vidId: 7n5ieHnu90w, vidTitle: BTS - I NEED U {Color coded lyrics Han|Rom|Eng}}, 8kyG5tTZ1iE: {Value: 1, vidImage: https://i.ytimg.com/vi/8kyG5tTZ1iE/mqdefault.jpg, vidId: 8kyG5tTZ1iE, vidTitle: SHINee 샤이니_Sherlock•셜록 (Clue + Note)_Music Video}, iy94tB5MldE: {Value: 1, vidImage: https://i.ytimg.com/vi/iy94tB5MldE/mqdefault.jpg, vidId: iy94tB5MldE, vidTitle: Simon D (사이먼디) - 짠해 (Cheerz) [MV ENG SUB]}, XuSYtAsMxfY: {Value: 1, vidImage: https://i.ytimg.com/vi/XuSYtAsMxfY/mqdefault.jpg, vidId: XuSYtAsMxfY, vidTitle: YOUTUBERS REACT TO K-Pop #3}, IZ1t7CwfvEc: {Value: 1, vidImage: https://i.ytimg.com/vi/IZ1t7CwfvEc/mqdefault.jpg, vidId: IZ1t7CwfvEc, vidTitle: GOT7 "Never Ever" M/V}, 2ips2mM7Zqw: {Value: 2, vidImage: https://i.ytimg.com/vi/2ips2mM7Zqw/default.jpg, vidId: 2ips2mM7Zqw, vidTitle: BIGBANG - 뱅뱅뱅 (BANG BANG BANG) M/V}, IlJHZJ8EqeA: {Value: 2, vidImage: https://i.ytimg.com/vi/IlJHZJ8EqeA/mqdefault.jpg, vidId: IlJHZJ8EqeA, vidTitle: MINO - '몸(BODY)' M/V}, 0Pinupmqwaw: {Value: 5, vidImage: https://i.ytimg.com/vi/0Pinupmqwaw/default.jpg, vidId: 0Pinupmqwaw, vidTitle: 직 재스퍼 (Zick Jasper) - PRIMETIME (Feat. 지구인)}}

最终计划是将此信息填充到Container中的Cards而不是文本中。

我没有收到错误,但这是“容器”的屏幕截图。和文字.. enter image description here

2 个答案:

答案 0 :(得分:0)

我不确定我理解你的问题,我认为你不需要FutureBuilderFirebaseAnimatedList已经为您提供了followerSnap的{​​{1}}参数中的数据。

P.S。一般情况下,您应该选择itemBuilder而非StreamBuilder而不是onValueFutureBuilder,这样您的数据就不会过时。

答案 1 :(得分:0)

我认为你在这里更多地询问如何迭代与问题标题无关的JSON对象,如果这是你要求的,你仍然应该遵循Collin的建议。

您声明数据值如下所示

{aZb6KxxIuWA: {Value: 1, vidImage: https://i.ytimg.com/vi/aZb6KxxIuWA/mqdefault.jpg, vidId: aZb6KxxIuWA, vidTitle: 10 Embarrassing K-POP Star Fails And Accidents}, 4408NthSJis: {Value: 1, vidImage: https://i.ytimg.com/vi/4408NthSJis/mqdefault.jpg, vidId: 4408NthSJis, vidTitle: [TOP 100] MOST VIEWED K-POP MUSIC VIDEOS • APRIL 2017}, 7n5ieHnu90w: {Value: 1, vidImage: https://i.ytimg.com/vi/7n5ieHnu90w/mqdefault.jpg, vidId: 7n5ieHnu90w, vidTitle: BTS - I NEED U {Color coded lyrics Han|Rom|Eng}}, 8kyG5tTZ1iE: {Value: 1, vidImage: https://i.ytimg.com/vi/8kyG5tTZ1iE/mqdefault.jpg, vidId: 8kyG5tTZ1iE, vidTitle: SHINee 샤이니_Sherlock•셜록 (Clue + Note)_Music Video}, iy94tB5MldE: {Value: 1, vidImage: https://i.ytimg.com/vi/iy94tB5MldE/mqdefault.jpg, vidId: iy94tB5MldE, vidTitle: Simon D (사이먼디) - 짠해 (Cheerz) [MV ENG SUB]}, XuSYtAsMxfY: {Value: 1, vidImage: https://i.ytimg.com/vi/XuSYtAsMxfY/mqdefault.jpg, vidId: XuSYtAsMxfY, vidTitle: YOUTUBERS REACT TO K-Pop #3}, IZ1t7CwfvEc: {Value: 1, vidImage: https://i.ytimg.com/vi/IZ1t7CwfvEc/mqdefault.jpg, vidId: IZ1t7CwfvEc, vidTitle: GOT7 "Never Ever" M/V}, 2ips2mM7Zqw: {Value: 2, vidImage: https://i.ytimg.com/vi/2ips2mM7Zqw/default.jpg, vidId: 2ips2mM7Zqw, vidTitle: BIGBANG - 뱅뱅뱅 (BANG BANG BANG) M/V}, IlJHZJ8EqeA: {Value: 2, vidImage: https://i.ytimg.com/vi/IlJHZJ8EqeA/mqdefault.jpg, vidId: IlJHZJ8EqeA, vidTitle: MINO - '몸(BODY)' M/V}, 0Pinupmqwaw: {Value: 5, vidImage: https://i.ytimg.com/vi/0Pinupmqwaw/default.jpg, vidId: 0Pinupmqwaw, vidTitle: 직 재스퍼 (Zick Jasper) - PRIMETIME (Feat. 지구인)}}

为了向您展示这是如何运作的,请将Text替换为FlatButton,并在_handleJson回调中调用onPressed方法_handleJson { {1}}作为参数,如下所示:

userSnap.data.value

,你的方法看起来像这样

   default:
if (userSnap.hasError)
    return new Text('Error: ${userSnap.error}');
else
    print(userSnap.data.value.toString());
    return new Container(
       new FlatButton(onPressed: _handleJson(userSnap.data.value),
          child: new Text("Load Data"),);

如果JSON主要编码,这应该有效,请告诉我这是否适用于您。