Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand Anonymous Function in Dart

Tags:

flutter

dart

I am trying to understand the code which is present in the line

list = (json.decode(response.body) as List)
          .map((data) => PhotoData.fromJson(data))
          .toList();

I have the following questions a) What does .map do ? b) What is 'data' in the above code c) Could you please simplify the code into long form so that I can better grasp it.

The complete function is listed as below:

_fetchData() async {

  http.Response response =
      await http.get("https://jsonplaceholder.typicode.com/photos");
  print("Fetching data...");
  list = (json.decode(response.body) as List)
      .map((data) => PhotoData.fromJson(data))
      .toList();
  setState(() {
    isLoading =false;
  });
}




factory PhotoData.fromJson(Map<String, dynamic> json) {
  return PhotoData( json['id'], json['title'],
      json['thumbnailUrl'], json['url']);
}
like image 935
Faizan Mir Avatar asked Feb 18 '26 00:02

Faizan Mir


1 Answers

map is a method inside class Iterable. Since class List implements EfficientLengthIterable<E> therefore it inherits the method map.

According to the docs, the map does the following:

Returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order.


This is the implemenation of the map method:

Iterable<T> map<T>(T f(E e)) => MappedIterable<E, T>(this, f);

Iterable<T> => this means that the map method will return an Iterable

f(E e) => means that map will contain a function with element of type E

=> the arrow is a shorthand of writing return


Example:

class Person 
{
  String firstName;
  String lastName;

  Person(this.firstName, this.lastName);
}

void main() {
    List<Person> people = new List<Person>();
  people.add(new Person("Joe", "Smithers"));
  people.add(new Person("Patrick", "Thomas"));
  var mappedNames = people.map<String>((Person n) => 'Mr. ${n.firstName} ${n.lastName}');
  print(mappedNames);
}

In this example map returns an iterable of type String, thus you can add map<String>, and variable n is of type Person, thus you can write Person n.

The type of n is equivalent to the type of variable people. Since here basically map method is iterating inside the list people and returning a new Iterable according to what you wrote in the return statement.

Therefore print(mappedNames) will give you the following:

(Mr. Joe Smithers, Mr. Patrick Thomas)

Note: you dont have to add the types since dart infers the type.


In your code you have the following:

list = (json.decode(response.body) as List)
          .map((data) => PhotoData.fromJson(data))
          .toList();

(json.decode(response.body) as List) which means that you are casting the json object to type List, then using map you iterate inside of it and return an iterable.

Then you use toList() to create a List from the returned iterable

like image 123
Peter Haddad Avatar answered Feb 20 '26 14:02

Peter Haddad