【问题标题】:flutter firebase database and ListView builder issue扑动 firebase 数据库和 ListView 构建器问题
【发布时间】:2019-02-14 05:43:57
【问题描述】:

我想在 ListView 中显示我的 shopList 的每个项目,但我找不到它一直显示相同记录的原因。请帮忙解决这个问题。

这里有代码,从firebase数据库中获取数据。

 databaseReference.once().then((DataSnapshot snapshot) {
    Map<dynamic, dynamic> getMap = snapshot.value;
    getMap.forEach((k, v) {

      Map<dynamic, dynamic> f = v;
      shop.key = k;
  shop.shopName = f["ShopName"];
  shop.tel = f["ShopTel"];
  shop.address = f["ShopAddress"];
  shop.thumbnail = f["Thumbnail"];
  debugPrint(k);

  shopList.add(shop);
  debugPrint(shopList[shopList.length-1].shopName);


});
  });

调试打印结果:

  • 颤振:-LLySYDHHHx9WtCvKPrO
  • 扑:shop1111
  • 颤动:-LLyXwR0nnAcx6_H4cYy
  • 颤振:shop2222

这里是数据库:

这里是 ListView 的代码:

child: Flexible(
                child: new ListView.builder(
                    itemCount: shopList.length,
                    itemBuilder: (context, index) {
                      return new Card(
                        color: Colors.red,
                        //elevation: 2.0,
                        child: new ListTile(

                          title: new Text("Name: ${shopList[index].key}"),

                        ),
                      );


                    }),
            ),

模拟器结果:

【问题讨论】:

  • 为什么不使用 Firebase 动画列表?你有一个叫做商店的对象吗?

标签: firebase listview firebase-realtime-database flutter builder


【解决方案1】:

试试这个。

我会这样做

Shop.dart

class Shop {
  String key;
  String name;
  String address;
  String phone;
  String thumbnail;

  Shop(this.name,this.address,this.phone,this.thumbnail);

  Shop.fromSnapshot(DataSnapshot snapshot)
      : key = snapshot.key,
        name = snapshot.value["name"],
        address= snapshot.value["address"],
        phone= snapshot.value["phone"],
        thumbnail= snapshot.value["thumbnail"];


  toJson() {
    return {
      "name": name,
      "address": address,
      "phone": phone,
      "thumbnail": thumbnail,
    };
  }
}

ma​​in.dart

List<Shop> itemsShop = List();
Shop itemShop;
DatabaseReference itemRefShop;

@override
void initState() {
  super.initState();
  itemShop = Shop("", "", "", "");
  final FirebaseDatabase database = FirebaseDatabase.instance;
  itemRefShop = database.reference().child('Shop');
  itemRefShop.onChildAdded.listen(_onEntryAddedShop);
  itemRefShop.onChildChanged.listen(_onEntryChangedShop);
}

_onEntryAddedShop(Event event) {
  setState(() {
    itemsShop.add(Shop.fromSnapshot(event.snapshot));
  });
}

_onEntryChangedShop(Event event) {
  var old = itemsShop.singleWhere((entry) {
      return entry.key == event.snapshot.key;
  });
  setState(() {
      itemsShop[Shop.indexOf(old)] = Shop.fromSnapshot(event.snapshot);
  });
}

 @override
  Widget build(BuildContext context) {
     return new Container(
        child: new Column(
          children: <Widget>[
             new Flexible(
              child: new FirebaseAnimatedList(
                     query: itemRefShop,
                     itemBuilder:(_, DataSnapshot snapshot, Animation<double> animation, int index){
                       return new ListTile(
                         title: new Text(snapshot.value['name']),
                         subtitle: new Text(itemsShop[index].address),
                       );
                     }
               ),
          ]
        ),
     );
  }
}

【讨论】:

  • 谢谢,我可以知道如何以及何时终止 firebase 侦听器吗?
  • _onEntryChangedShop 事件告诉您已更改的事件,_onEntryAddedShop 告诉您添加它们的事件。您可以使用 print(event.snapshot.value);在每一次聆听中
  • 对于 swift 和 kotlin,我们必须停止监听器。我们应该在某个地方停止监听器吗?
猜你喜欢
  • 2021-06-23
  • 2018-01-02
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 2018-04-21
相关资源
最近更新 更多