Please help me understand fromMap and toMap from this code?
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 variableand 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 apilike
Abc a = Abc(name:"hari",address:"india");a.toMap();