扑扑firebase数据库和ListView构建器问题 [英] flutter firebase database and ListView builder issue

查看:79
本文介绍了扑扑firebase数据库和ListView构建器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ListView中显示我的shopList的每个项目,但是我无法找出为什么它继续显示相同记录的原因.请帮助解决此问题.

I suppose to show each item of my shopList in ListView, but I can't find out the reason why it keep showing the same record. Please help to solve this issue.

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

Here with the code, get data from firebase database.

 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);


});
  });

DebugPrint结果:

DebugPrint result:

  • 颤振:-LLySYDHHHx9WtCvKPrO
  • 颤抖:shop1111
  • 颤振:-LLyXwR0nnAcx6_H4cYy
  • 颤抖:shop2222

有关数据库:

以下是ListView的代码:

Here with the code for 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}"),

                        ),
                      );


                    }),
            ),

模拟器的结果:

推荐答案

尝试一下.

那就是我要做的

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,
    };
  }
}

main.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数据库和ListView构建器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆