扑火基础数据库initState问题

时间:2018-09-13 16:56:47

标签: database firebase flutter

我的应用程序有一个对象Company,该公司的属性shopList。该商店清单将从void initState()中的Firebase数据库获取数据。

Company(name: 'Storm',
      about: 'xxxxxxxxxxxx',
      backdropPhoto: 'assets/hk.jpg',
      shopList: [],
      location: 'HK',
      logo: 'assets/logo.png',
      president: 'Grand Production House');

商店清单应该有5家不同的商店,但我不知道为什么会有5家商店使用相同的数据。

代码:

class CompanyDetailsPage extends StatefulWidget {

    CompanyDetailsPage(
      {@required AnimationController controller, this.context})
      : animation = new CompanyDetsIntroAnimation(controller);

  final BuildContext context;

  final CompanyDetsIntroAnimation animation;
  @override
  _CompanyDetailsPageState createState() => _CompanyDetailsPageState();
}

class _CompanyDetailsPageState extends State<CompanyDetailsPage> {

  Shop shopItems;

  Company storm = Company(
      name: 'Storm',
      about: 'xxxxxxxxxxxx',
      backdropPhoto: 'assets/hk.jpg',
      shopList: [],
      location: 'HK',
      logo: 'assets/logo.png',
      president: 'Grand Production House');

  DatabaseReference databaseReference = FirebaseDatabase.instance.reference();

  @override
  void initState() {
    super.initState();

    shopItems = Shop();

    databaseReference.child('HK').once().then((DataSnapshot snapshot) {
      Map uid = snapshot.value;
      uid.forEach((k,v) {
        Map shopMap = v['Shop'];

        shopMap.forEach((sk,sv) {
          shopItems.key = sk;
          shopItems.shopName = sv["ShopName"];
          shopItems.address = sv["ShopAddress"];
          shopItems.tel = sv["ShopTel"];
          shopItems.thumbnail = sv["Thumbnail"];


          debugPrint(shopItems.address);

          storm.shopList.add(shopItems);

          debugPrint(shopItems.key);
        });

      });

      for (int i = 0; i < storm.shopList.length; i++) {

        debugPrint("Username: ${storm.shopList[i].address }, User Id: ${storm.shopList[i].key}");

      }
    });
  }

enter image description here

控制台结果

  • 正在将文件同步到设备iPhone X ...
  • 颤振:-LM3JFMq5y9fNVA431QW
  • 颤振:-LMHR9YQFqgKlnFArwEN
  • 颤振:-LM3JH8KMha3aeN-YEq5
  • 颤振:-LM3JJTFda0c3qKaKEaL
  • 颤振:-LMIaUIBOhj1k6pjj9eY
  • flutter:商店地址:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • flutter:商店地址:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • flutter:商店地址:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • flutter:商店地址:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • flutter:商店地址:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY

0 个答案:

没有答案