I got this code from the internet and I can not seem to understand it or find anything on the internet for it.
In the code below toMap is a method that returns 2 items, How is that possible?
And what is fromMap, is it a user created method? I thought methods used {} or => so it is a bit confusing.
Also, what is the key here for the Map? Can the map only store 2 categories of items? One is the key and the other is the value. Or it can have one key but multiple categories of values.
For example, there might be a single unique key, which could help take out the task title, time, reminder data, notes, etc as values of the map.
class Task {
String title;
bool completed;
Task({
this.title,
this.completed = false,
});
Task.fromMap(Map<String, dynamic> map): title = map['title'],completed = map['completed'];
updateTitle(title) {
this.title = title;
}
Map toMap() {
return {
'title': title,
'completed': completed,
};
}
}
In the code below toMap is a method that returns 2 items, How is that possible?
No, it returns a Map
(with two items). More about maps can be found here.
And what is fromMap, is it a user created method? I thought methods used {} or => so it is a bit confusing.
Task.fromMap(Map<String, dynamic> map)
is called "named constructor". The : title = map['title'],completed = map['completed']
part is initializer list
My understanding is;
In fromMap
, you retrieve the title and completed from some map, and save it in your local variables.
In the toMap
you take the saved values in your local variables and can return a Map.
The key is whatever you put you chose it to be, but here you chose one key to be title
and one to be completed
.
Does this help you?
First of all we discuss about FormMap
So what is fromMap()?
whenever you have any api and at firetime you will get json format so when you want to convert that data into any class format then you have to do like
Map temp = json.decode(response.body);
so your function can understand map key and retrieve that value and set in class local variable
and now Second point is toMap
So what is toMap()?
Whenever you want to post something into api or somewhere you have map data so you can post in api
like
Abc a = Abc(name:"hari",address:"india");
a.toMap();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With