Skip to content
Snippets Groups Projects
Commit 38003e32 authored by Yuki's avatar Yuki
Browse files

Task 2 part 1:

1. Download API data
2. Filter invalid data
3. Convert json obj into Characters
4. Display data
parent d8182c5d
No related branches found
No related tags found
No related merge requests found
...@@ -8,4 +8,29 @@ class Character { ...@@ -8,4 +8,29 @@ class Character {
String name; String name;
String gender; String gender;
List<String> aliases; List<String> aliases;
static List<Character> listFromJson(List<dynamic> jsonList) {
final data = <Character>[];
for (dynamic person in jsonList) {
if ((person['name'].isNotEmpty) && (person['gender'].isNotEmpty) && (person['aliases'].length > 0)) {
// print("//// DEBUG // person['name']: ${person['name']}");
// print("//// DEBUG // person['gender']: ${person['gender']}");
// print("//// DEBUG // person['aliases'].length: ${person['aliases'].length}");
// print("//// DEBUG // person['aliases'].runtimeType: ${person['aliases'].runtimeType}"); // List<dynamic>
final aliases = (person["aliases"] as List).map((e) => e as String).toList();
// print('//// DEBUG // method 1 prints: $aliases');
// print('//// DEBUG // method 1 prints: ${aliases.runtimeType}'); // List<String>
data.add(Character(
name: person['name'],
gender: person['gender'],
aliases: aliases,
));
}
}
return data;
}
} }
// The class is responsible to load the GoT characters from the GoTAPI (on https://anapioficeandfire.com/) // The class is responsible to load the GoT characters from the GoTAPI (on https://anapioficeandfire.com/)
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:assignment_1_base_project/db/character.dart'; import 'package:assignment_1_base_project/db/character.dart';
class API { class API {
// Note: the documentation for the API can be found here: https://anapioficeandfire.com/Documentation // Note: the documentation for the API can be found here: https://anapioficeandfire.com/Documentation
final String _charactersRoute = final String _charactersRoute = "https://anapioficeandfire.com/api/characters";
"https://anapioficeandfire.com/api/characters";
Map<String, dynamic> extractFields(Map<String, dynamic> json) {
return {
'name': json['name'],
'gender': json['gender'],
// 'aliases': json['aliases'],
};
}
// Loads the list of GoT characters // Loads the list of GoT characters
Future<List<Character>> fetchRemoteGoTCharacters() { Future<List<Character>> fetchRemoteGoTCharacters() async {
// TODO: Load GoT characters from the _charactersRoute and return them. // TODO: Load GoT characters from the _charactersRoute and return them.
// For the API calls we recommend to use the 'http' package // For the API calls we recommend to use the 'http' package
//*** Must be replaced List<Character> _characters = [];
return Future.delayed(
Duration(seconds: 3), try {
() => [ var uri = Uri.parse(_charactersRoute);
Character( var response = await http.get(uri);
name: "Jon Snow", // print("//// DEBUG // response.body.runtimeType:${response.body.runtimeType}"); // String
gender: "Male", // print(//// DEBUG // response.body);
aliases: [
"Lord Snow", if (response.statusCode == 200) {
"Ned Stark's Bastard", final jsonList = jsonDecode(response.body);
"The Snow of Winterfell", // print("jsonList.runtimeType:${jsonList.runtimeType}"); // List<dynamic>
"The Crow-Come-Over", // print("//// DEBUG // jsonList.length:${jsonList.length}"); // 10
"The 998th Lord Commander of the Night's Watch",
"The Bastard of Winterfell", _characters = Character.listFromJson(jsonList);
"The Black Bastard of the Wall", } else {
"Lord Crow" print("Error: Fail to connect the API.( statusCode : ${response.statusCode} )");
], }
), } catch (e) {
Character( print("Exceptions error: ( ${e} )");
name: "Brandon Stark", }
gender: "Male",
aliases: ["Bran", "Bran the Broken", "The Winged Wolf"], // print("//// DEBUG // _characters.length: ${_characters.length} - number of valid data");
), // print("//// DEBUG // _characters:${_characters}");
Character(
name: "Margaery Tyrell", return _characters;
gender: "Female",
aliases: [
"The Little Queen",
"The Little Rose",
"Maid Margaery",
],
),
],
);
//*** Replace up to here
} }
} }
...@@ -23,6 +23,7 @@ environment: ...@@ -23,6 +23,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
http: ^1.0.0
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment