2

I have two tables in Supabase, one called places which stores just basic information of the place such as name etc. The other called places_information which stores addresses and other details.

A place could have many addresses such as a franchise so it has a one to many relationship, with a custom id field being created in places_information that is linked to the main id in the places table

I have made dummy data where two addresses have been made linked to one place

Code

    await supabase.from('places').select('''*,
        places_information (*)''').execute().then((value) {
          if (value.error != null) {
            context.showSnackBar(message: 'Places dont exist');
          } else {
            log(value.data[0].toString());
          }
        });

Using the code above returns the following output

{
   "id":4,
   "name":"Just a name",
   "description":"This is a description",
   "cuisine":"Steakhouse",
   "places_information":[
      {
         "id":1,
         "place_id":4,
         "street_address":"test data",
         "city":"another place",
      },
      {
         "id":4,
         "place_id":4,
         "street_address":"second data",
         "city":"second place",
      }
   ]
}

Where as i just want

{
   "id":4,
   "name":"Just a name",
   "description":"This is a description",
   "cuisine":"Steakhouse",
   "places_information":[
      {
         "id":1,
         "place_id":4,
         "street_address":"test data",
         "city":"another place",
      }
   ]
},
{
   "id":4,
   "name":"Just a name",
   "description":"This is a description",
   "cuisine":"Steakhouse",
   "places_information":[
      {
         "id":4,
         "place_id":4,
         "street_address":"second data",
         "city":"second place",
      }
   ]
}
iKreateCode
  • 189
  • 3
  • 13

1 Answers1

0

You can iterate over the value.data list and create a new list that contains each element of places_information as a separate dictionary. For example:


List<Map<String, dynamic>> newData = [];
for (var i = 0; i < value.data.length; i++) {
  for (var j = 0; j < value.data[i]["places_information"].length; j++) {
    var temp = Map<String, dynamic>.from(value.data[i]);
    temp["places_information"] = [value.data[i]["places_information"][j]];
    newData.add(temp);
  }
}

In this code, newData is a list that will contain the new dictionaries with separate places_information values. The outer loop iterates over each element of value.data, while the inner loop iterates over each element of the places_information list.

For each places_information element, a new dictionary is created (temp) with the same values as the original dictionary (value.data[i]), except for the places_information value, which is replaced with a new list containing only the current places_information element. This new dictionary is then added to newData.

After running this code, newData will contain the desired list of dictionaries with separate places_information values.

Mansueli
  • 6,223
  • 8
  • 33
  • 57