Flutter/Dart/Firebase - Updated nested map values

328

I would recommend to refactor your database structure because the first problem you will hit with that one is that firestore for now does not support updating a specific index for an array field value. You can get more info about that here.

You could get the whole value individual_ticket_sales update it and save it again as whole but it would be just a matter of time when you would hit the problem that multiple users want to update the same value on almost the same time and one of the changes get's lost. Even the usage of transaction would not be 100% safe because of the size of the object and potential multiple changes.

Is it possible for you to store each ticketId as a firestore document in a firestore collection like this:


FirebaseFirestore.instance
          .collection('giveaways')
          .doc(_items.values.toList()[i].giveaway.giveawayId)
          .collection('individual_ticket_sales')
          .doc(i)
          .update(user.uid);
Share:
328
TJMitch95
Author by

TJMitch95

Updated on December 29, 2022

Comments

  • TJMitch95
    TJMitch95 over 1 year

    I am trying to record which on my users has purchased which ticket in my app. I am using firebase to store my data about my users and giveaways. When a purchase is complete, I am trying to update the relevant giveaway and assign each ticket to a user using their id.

    Firstly, I am not sure if my data schema is the most appropriate for what I'm trying to achieve so open to suggestions for editing as I'm still quite new to the flutter world.

    Second, here is how my data is currently structured: enter image description here

    Here is how I have structured my code. Here is my SingleBasketItem model:

     class SingleBasketItem {
      final String id;
      final String uid;
      final OurGiveAways giveaway;
      final int ticketNumber;
      SingleBasketItem(this.id, this.uid, this.giveaway, this.ticketNumber);
    }
    

    Here is my Shopping Basket model, I have added an Elevated Button to my shopping basket page which, when tapped, will execute the placeOrder() function:

        class ShoppingBasket extends ChangeNotifier {
      Map<String, SingleBasketItem> _items = {};
    
      Map<String, SingleBasketItem> get items {
        return {..._items};
      }
    
      void addItem(String id, String uid, OurGiveAways giveaway, int ticketNumber) {
        _items.putIfAbsent(
          id,
          () => SingleBasketItem(id, uid, giveaway, ticketNumber),
        );
        notifyListeners();
      }
    
      void placeOrder(BuildContext context, OurUser user) {
        for (var i = 0; i < _items.length; i++) {
          FirebaseFirestore.instance
              .collection('giveaways')
              .doc(_items.values.toList()[i].giveaway.giveawayId)
              .update(
            {
              'individual_ticket_sales'[_items.values.toList()[i].ticketNumber]:
                  user.uid,
            },
          );
        }
      }
    }
    

    Below is an image of the results:

    enter image description here

    By analysing the results it looks like my code is creating a new field with a title of the 1st index character of individual_ticket_sales ("n" because ive bought ticket 1), how can I set the nested "1" (or whatever ticket I choose) to my user id rather than creating a new field? Thanks.