我的应用程序有一个对象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}");
}
});
}
控制台结果