Can a map key be a map itself?

40 Views Asked by At

I have created a map, but I want some of the keys (locations & jobs) to have multiple values. Is it best practice to create a new "Client" for each new kay value, or can I make the locations & jobs maps? I have this:

class Client {
  String? name;
  String? status;
  String? location;
  String? job;
  String? notes;
  String? priority;
  String? estimate;
  String? doe;
  int? time;
Client.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    status = json['status'];
    location = json['location']; // multiple locations per client (name)
    job = json['job']; // multiple jobs per client & location combo
    notes = json['notes'];
    priority = json['priority'];
    estimate = json['estimate'];
    doe = json['doe'];
    time = json['time'];
  }
final map = {
                              "name": nameController.text,
                              "location": locationController.text,
                              "job": jobController.text,
                              "notes": notesController.text,
                              "priority": priority,
                              "status": status,
                              "time": DateTime.now().millisecondsSinceEpoch,
                            };

I essentially want this output, but I'm not sure which path to take:

name1, location1, jobA

name1, location1, jobB

name1, location2, jobC

name2, location2, jobD

I haven't tried anything yet. I am very new to coding and don't know what would be best for the database functionality.

2

There are 2 best solutions below

0
Vladimir Gadzhov On

You have a good approach with the Client class, you may continue using the same approach with location and job, if they are complex objects.

class Location {
   // Add your fields here

   Location.fromJson(Map<String, dynamic> map) {
       // assign properties to the map values
   }
}

class Job {
   // Add your fields here

   Job.fromJson(Map<String, dynamic> map) {
       // assign properties to the map values
   }
}

And then use them in the Client

Client.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    status = json['status'];
    location = List.from(json['location']).map((locationMap) => Location.fromJson(locationMap)).toList(),
    job = List.from(json['job']).map((jobMap) => Job.fromJson(jobMap)).toList,
    // Other fields...
}

If you don't have a complex objects, then just use a List<Object> (Replace object with the proper type).

p.s. You mentioned database, so You can implement toJson method, which serialize all your objects back to a Map, so you can easily store them inside a db.

Map<String, Object> toJson() {
    return <String, Object>{
        'fieldName': value,
        // include all fields and their values
    };
}
0
Lida On

Yes, it can. Maps can have keys of any type, including other maps. This allows to have flexible and nested data structures.

For example:

void main() {
  final nestedMapKey1 = {'key1': 1, 'key2': 2};

  Map<dynamic, String> nestedMap = {
    nestedMapKey1: 'value1',
    'mapKey2': 'value2',
  };

  print(nestedMap[nestedMapKey1]); // output: value1
}

But this is not the best practice for this case. It would be much better to create separate classes for the fields job and location. Like this:

class Client {
  String? name;
  String? status;
  Location? location;
  Job? job;
  String? notes;
  String? priority;
  String? estimate;
  String? doe;
  int? time;
  
  Client.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    status = json['status'];
    location = json["location"] == null
            ? null
            : Location.fromJson(json["location"]);
    job = json["job"] == null
            ? null
            : Job.fromJson(json["job"]);
    notes = json['notes'];
    priority = json['priority'];
    estimate = json['estimate'];
    doe = json['doe'];
    time = json['time'];
  }
}

class Location {
  String? property1;
  String? property2;
  
  Location.fromJson(Map<String, dynamic> json) {
    property2 = json['property1'];
    property2 = json['property2'];
  }
}

class Job {
  String? property1;
  String? property2;
  
  Job.fromJson(Map<String, dynamic> json) {
    property2 = json['property1'];
    property2 = json['property2'];
  }
}

But if you really need to have just multiple values for the job and location, then you can also make the type of these fields a List.

class Client {
...
    List<String>? location;
    List<String>? job;
...