database - 颤动的firebase数据库initState问题
问题描述
我的应用程序有一个具有属性 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
解决方案
我认为问题可能出在您正在实例化的地方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);
});
推荐阅读
- python - 使用 kv lang 插入图形
- reporting-services - 当一个或多个值都没有时的SSRS Lookupset求和问题
- python - 将所有非黑色像素转换为一种颜色不会产生预期的输出
- python - 导入 python 脚本并传递参数以在不同的脚本中运行
- hyperledger-fabric - Hyperledger Fabric:对等链代码调用:将 JSON 对象传递给链代码函数?
- vb.net - 这段代码有什么问题?我无法从我的数据库中检索数据
- hyperledger-fabric - 如何在超级账本作曲家中获取特定资产的交易历史?
- c# - 使用 c# 将集合并行拆分为相等的批次
- r - 获取未过滤向量中元素的索引
- docker - 从批处理脚本调用 cmake(在 docker 内)