0

I want to create an Object from an object type defined in Type or from an object name defined in String. The following example uses a String to hold the object type. But I think this is not that elegant - even with Type, this if block would increase dramatically for a lot of object types...

I didn't find a better solution for this yet. How can I create that object dynamically from the specified object type?

if (model == 'Event') {
  data = Event.fromMap(result);
}
else if (model == 'Content') {
  data = Content.fromMap(result);
}
else if (...) {
  // ...
}
2
  • You can't, but you could compress your code with a switch statement in a function. Each case contains just a return. Commented Feb 16, 2020 at 14:07
  • Ok, thank you for the answer. It's not ideal, but at least a small optimization to my code above... Commented Feb 18, 2020 at 13:22

2 Answers 2

1

This is other approach.

class Event{
  Event.fromMap(_map){
    print('This is an event');
    print(_map);
  }
}

class Content{
  Content.fromMap(_map){
    print('This is a content');
    print(_map);
  }
}

Map<String, Function> types = {
  'Event' : (_map)=>Event.fromMap(_map),
  'Content' : (_map)=>Content.fromMap(_map),
};

void main() {
  var a = types['Event']({'test':'success_event'});
  print(a.runtimeType);
  var b = types['Content']({'test':'success_content'});
  print(b.runtimeType);
}

Its a bit more scalable (since only depends on add the class constructor into the map).

The explanation:

class Event{
  Event.fromMap(_map){
    print('This is an event');
    print(_map);
  }
}

class Content{
  Content.fromMap(_map){
    print('This is a content');
    print(_map);
  }
}

Here we are creating the test classes. nothing important.

Map<String, Function> types = {
  'Event' : (_map)=>Event.fromMap(_map),
  'Content' : (_map)=>Content.fromMap(_map),
};

Here we are defining a Map. Why? Because it allows us to access some value through some key in constant time. In this case, the keys are the Strings 'Event', 'Content', but also can be types as you wanted. For simplicity, let them be Strings. The values are Function's, in this example only getting as parameter a _map (because the Class constructors in the example require one parameter _map). So, if you need more types, only add the type and the function encapsulating the constructor for that type.

void main() {
  var a = types['Event']({'test':'success_event'});
  print(a.runtimeType);
  var b = types['Content']({'test':'success_content'});
  print(b.runtimeType);
}

Finally you can instantiate the classes easily. Only with the type string and passing to the function the values you want (In this example a map with a key 'test').

In your example would be something like:

data = types[model](result);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. This might be a solution I can life with.
0

I also got stuck with a similar problem. Here's how I fixed it:

Create Objects of a class :

class a{ 
a.fromMap(_map){}}

class b{
b.fromMap(_map){}}

Map<String,Dynamic> obj (){
return { 
"a":a,
"b":b}

Now we can utilize the obj object to perform the desired operations

Hope this helps

Comments

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.