flutter firebase database initState issue
My app has a object Company that has an attribute shopList. The shoplist will get data from firebase database in void initState() .
Company(name: 'Storm',
about: 'xxxxxxxxxxxx',
backdropPhoto: 'assets/hk.jpg',
shopList: [],
location: 'HK',
logo: 'assets/logo.png',
president: 'Grand Production House');
The shoplist is supposed to have 5 different shops, but I don't know why it will have 5 shops with the same data.
Code:
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}");
}
});
}
Result from console:
Syncing files to device iPhone X...
flutter: -LM3JFMq5y9fNVA431QW
flutter: -LMHR9YQFqgKlnFArwEN
flutter: -LM3JH8KMha3aeN-YEq5
flutter: -LM3JJTFda0c3qKaKEaL
flutter: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
I think the problem may be where you are instantiating shopItems = Shop();.
Try removing it from there and do this
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);
});
Company(name: 'Storm',
about: 'xxxxxxxxxxxx',
backdropPhoto: 'assets/hk.jpg',
shopList: [],
location: 'HK',
logo: 'assets/logo.png',
president: 'Grand Production House');
The shoplist is supposed to have 5 different shops, but I don't know why it will have 5 shops with the same data.
Code:
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}");
}
});
}
Result from console:
Syncing files to device iPhone X...
flutter: -LM3JFMq5y9fNVA431QW
flutter: -LMHR9YQFqgKlnFArwEN
flutter: -LM3JH8KMha3aeN-YEq5
flutter: -LM3JJTFda0c3qKaKEaL
flutter: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
flutter: ShopAddress: bbbbbb, ShopKey: -LMIaUIBOhj1k6pjj9eY
I think the problem may be where you are instantiating shopItems = Shop();.
Try removing it from there and do this
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);
});
Comments
Post a Comment