簡體   English   中英

如何在Flutter中的Firebase快照上調用Future?

[英]How to call a Future on a Firebase snapshot in Flutter?

使用StreamBuilder返回快照時,出現“僅在null上調用快照”的快照時,只會出現一兩秒鍾的重復錯誤。 我相信我需要使快照異步,但是我可能完全錯了。 這是我的代碼...

child: new StreamBuilder(
                        stream: fb.child('UserVideo/${fUser.uid}').orderByKey().onValue,
                        builder: (BuildContext context, AsyncSnapshot<Event> event) {
                          if (event.data.snapshot.value == null) {
                            return new Card(
                              child: new Center(
                                child: new Text('SEARCH',
                                    textAlign: TextAlign.center,
                                    style: new TextStyle(
                                        fontSize: 30.0,
                                        fontFamily: 'Chewy',
                                        color: Colors.black)),
                              ),
                            );
                          } else if (event.data.snapshot.value != null) {
                              Map unsorted =
                                  event.data.snapshot.value; //store each map

                              final myMap =
                              new SplayTreeMap<String, dynamic>.from(
                                  unsorted,
                                      (a, b) => unsorted[a]['rank']
                                      .compareTo(unsorted[b]['rank']));

                              //Map myMap = event.data.snapshot.value; //store each map
                              var titles = myMap.values;

                              List onesTitles = new List();
                              List onesIds = new List();
                              List onesImages = new List();
                              List onesRank = new List();

                              for (var items in titles) {
                                onesTitles.add(items['title']);
                                onesIds.add(items['videoID']);
                                onesImages.add(items['imageString']);
                                onesRank.add(items['rank'].toString());
                              }

                              names = onesTitles;
                              ids = onesIds;
                              numbers = onesRank;
                              vidImages = onesImages;

我正在將我的用戶信息排列到列表中,然后從中填充卡信息。

使用安全導航操作符應防止該錯誤

event.data?.snapshot?.value

僅當之前的部分求值為!= null才顯示后續部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM