0

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

      }
    });
  }

enter image description here

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
2
  • You should try naming variables with more descriptive words. It's hard to follow like this. If you debug the method, can you see the five different shops returned by firebase? Commented Sep 13, 2018 at 20:53
  • debugPrint(shopItems.address) print out 5 different address, then I put this shopItem in to shopList array of Company object. But I print the element of shopList array, it shows that all the items are the same. I couldn't find out whats the problem. Commented Sep 14, 2018 at 5:01

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! May I ask why the shopItem didn't change value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.