【问题标题】:flutter firebase database initState issue颤动的firebase数据库initState问题
【发布时间】:2019-02-18 11:13:41
【问题描述】:

我的应用程序有一个具有属性 shopList 的对象公司。 shoplist 将在 void initState() 中从 firebase 数据库获取数据。

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

shoplist 应该有 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}");

      }
    });
  }

控制台结果:

  • 正在将文件同步到设备 iPhone X...
  • 颤振:-LM3JFMq5y9fNVA431QW
  • 颤振:-LMHR9YQFqgKlnFArwEN
  • 颤振:-LM3JH8KMha3aeN-YEq5
  • 颤振:-LM3JJTFda0c3qKaKEaL
  • 颤动:-LMIaUIBOhj1k6pjj9eY
  • 颤振:ShopAddress:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • 颤振:ShopAddress:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • 颤振:ShopAddress:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • 颤振:ShopAddress:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY
  • 颤振:ShopAddress:bbbbbb,ShopKey:-LMIaUIBOhj1k6pjj9eY

【问题讨论】:

  • 您应该尝试使用更具描述性的词语来命名变量。很难像这样跟随。如果你调试方法,你能看到firebase返回的五个不同的商店吗?
  • debugPrint(shopItems.address) 打印出 5 个不同的地址,然后我将这个 shopItem 放入 Company 对象的 shopList 数组中。但是我打印了 shopList 数组的元素,它表明所有的项目都是一样的。我找不到问题所在。

标签: database firebase flutter


【解决方案1】:

我认为问题可能出在您实例化 shopItems = Shop(); 的位置。

尝试从那里删除它并执行此操作

shopMap.forEach((sk,sv) {
//Create the instance here
  shopItems = Shop();

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

【讨论】:

  • 非常感谢!请问为什么shopItem没有改变值?
猜你喜欢
  • 2021-05-06
  • 1970-01-01
  • 2019-03-23
  • 2019-03-16
  • 2021-06-30
  • 1970-01-01
  • 2023-03-06
  • 2017-04-20
  • 1970-01-01
相关资源
最近更新 更多