如何在Flutter中使用FirebaseAnimatedList在FirebaseQuery上正确执行未来?

时间:2017-11-12 19:55:04

标签: firebase-realtime-database async-await dart future flutter

我最初查看FirebaseAnimatedList时,我的FirebaseQuery未正确等待信息。一旦我离开页面并按预期返回一切。这是我的代码......

var fb = FirebaseDatabase.instance.reference();

var allUsers = FirebaseDatabase.instance
    .reference()
    .child('users')
    .orderByChild('displayName');

@override
  Future <Null> _getFriends(value) async {
    try {
      Map myMap = await value; //store each map
      String n;
      String p;

      var titles = await myMap.values;
      for (var items in titles) {
        n = (items['displayName']);
        p = (items['photo']);
      }
      await name.add(n);
      await photos.add(p);
    } catch (error) {}
  }

child: new FirebaseAnimatedList(
                          query: fb,
                          padding: new EdgeInsets.all(15.0),
                          //sort: ,
                          reverse: false,
                          itemBuilder: (_, DataSnapshot usersSnap,
                              Animation<double> animation, int Index) {
                            return new StreamBuilder<Event>(
                                stream: fb
                                    .child('users')
                                    .orderByChild('displayName')
                                    .onValue,
                                builder: (BuildContext context,
                                    AsyncSnapshot<Event> event) {
                                  switch (event.connectionState) {
                                    case ConnectionState.none:
                                      _getFriends(event.data.snapshot.value);

                                      return new InkWell(
                                        splashColor: Colors.blueAccent,
                                        onTap: null,
                                        child: new ListTile(
                                            leading: new CircleAvatar(
                                              backgroundImage: new AssetImage(
                                                  'assets/placeholder.png'),
                                              radius: 35.0,
                                            ),
                                            title: new Text(name[Index],
                                              style: new TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                  fontSize: 20.0,
                                                  color: Colors.black
                                              ),)
                                        ),
                                      );

0 个答案:

没有答案