1

I'm having trobule with adding Array of Objects (tables) to Array of objects(restaurants). By that i mean, that I have following Firestore structure:

... (other data)
restaurants: [
   0: {
      name: name_0
      ...
      tables: [
          0: { no_chairs: 5, ... },
      ]
   },
   ...
]

I know how to add new object to restaurants, but I'm having trouble with adding new restaurant if tables list contains any data.

My code rn:

    var collection = firestore.collection('usersData')
        .doc(this.currentUser.uid).collection('buisness').doc('restaurants');
    collection.update({
      'restaurants': FieldValue.arrayUnion([restaurant.toJson()]),
    }).then( ... // reaction based on api answer
class Restaurant:
     Map<String, dynamic> toJson() => {
    'name': name,
    ...
    'tables': tables!=null? [tables] : [], // I think that I should modify this line
  };

class Table:
    Map<String, dynamic> toJson()=> { // Or this method / use another
    'no_chairs': no_chairs,
    ...
  };

Is there any simple way to do it? Preferably without modyfing firestore file structure, but if it is necessary I'm willing to do it.


Extra question: How to modify object in tables list? For example I want to change no_chairs to 7

1 Answer 1

2

From your code above, the line below has a problem:

'tables': tables!=null? [tables] : [], 

This part [tables] means you're putting the list tables as an element of the outer list.

To illustrate, if tables was as shown below:

List<int> tables = [1, 2, 3];

Then, [tables] is this:

[[1, 2, 3]]

Solution:

You can just use the tables variable directly since you're sending a list.

Before sending your tables object, you need to convert it to a list of Maps from the list of TableModel that is currently is (according to your comment below).

So you can modify your code to this:

'tables': tables != null ? tables.map((TableModel table) => table.toJson()).toList() : [], 

Extra question: How to modify object in tables list? For example I want to change no_chairs to 7

Solution: You will have to get the actual list of restaurants and modify the exact table object you want and set the modified list to Firestore.

 var collection = firestore.collection('usersData').doc(this.currentUser.uid).collection('buisness').doc('restaurants');

 //Get list of restaurants
 List<Restaurant> restaurantList = ((await collection.get()).data()).map((dynamic restaurantJson) => Restaurant.fromJson(restaurantJson)).toList();

 Set number of chairs
 //
 restaurantList[restaurantIndex].tables[tableIndex].no_chairs = 7; //This assumes the no_chairs field is not final

 //Set list 
 collection.set({
   "restaurants": restaurantList.toJson()
 })

Sign up to request clarification or add additional context in comments.

4 Comments

Without [] I get error: Failed to add restaurant: Invalid argument: Instance of 'TableModel' as response from firebase
Alright, I'll make an update with a fix for this.
@next_99, please check out the updated answer.
Works great. Could you also help with extra question? - Editing 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.