Update Response and Requests authored by Nicholas Sarian's avatar Nicholas Sarian
......@@ -77,6 +77,18 @@ factory BasicResponse.fromJson({
```
In the code above, a factory constructor is used to return an instance of the BasicResponse class. This factory constructor takes a JSON object as a parameter and returns a new BasicResponse instance with the success and description variables initialized from the JSON data.
### Converting from Json into a List
```dart
mapNames: (json['mapNames'] as List)
.map((e) => e.toString())
.toList(),
```
This code snippet is a part of the implementation of the fromJson() method in the [quest response](https://gitlab.engr.ship.edu/merlin/freshmanrpgsuite/-/blob/main/game_manager/lib/repository/quest/quest_editing_response.dart) page. The code snippet extracts the "mapNames" property of the JSON object and casts it to a List of dynamic objects using the `as List` operator.
The `map()` method is then called on the List, which applies a lambda function to each element of the List. The lambda function takes an argument `e`, representing each element in the List, and converts it to a String using the toString() method. The resulting transformed elements are returned as an iterable object.
Finally, the toList() method is called on the iterable object to convert it into a List of Strings, which is then assigned to the mapNames variable inside the QuestResponse object being created by the fromJson() method
# A Quick Note on the Constructors
There are two types of constructors you can use, one with {} and one without:
......
......