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
Branches changed_Id
No related tags found
No related merge requests found
......@@ -8,4 +8,29 @@ class Character {
String name;
String gender;
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/)
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:assignment_1_base_project/db/character.dart';
class API {
// Note: the documentation for the API can be found here: https://anapioficeandfire.com/Documentation
final String _charactersRoute =
"https://anapioficeandfire.com/api/characters";
final String _charactersRoute = "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
Future<List<Character>> fetchRemoteGoTCharacters() {
Future<List<Character>> fetchRemoteGoTCharacters() async {
// TODO: Load GoT characters from the _charactersRoute and return them.
// For the API calls we recommend to use the 'http' package
//*** Must be replaced
return Future.delayed(
Duration(seconds: 3),
() => [
Character(
name: "Jon Snow",
gender: "Male",
aliases: [
"Lord Snow",
"Ned Stark's Bastard",
"The Snow of Winterfell",
"The Crow-Come-Over",
"The 998th Lord Commander of the Night's Watch",
"The Bastard of Winterfell",
"The Black Bastard of the Wall",
"Lord Crow"
],
),
Character(
name: "Brandon Stark",
gender: "Male",
aliases: ["Bran", "Bran the Broken", "The Winged Wolf"],
),
Character(
name: "Margaery Tyrell",
gender: "Female",
aliases: [
"The Little Queen",
"The Little Rose",
"Maid Margaery",
],
),
],
);
//*** Replace up to here
List<Character> _characters = [];
try {
var uri = Uri.parse(_charactersRoute);
var response = await http.get(uri);
// print("//// DEBUG // response.body.runtimeType:${response.body.runtimeType}"); // String
// print(//// DEBUG // response.body);
if (response.statusCode == 200) {
final jsonList = jsonDecode(response.body);
// print("jsonList.runtimeType:${jsonList.runtimeType}"); // List<dynamic>
// print("//// DEBUG // jsonList.length:${jsonList.length}"); // 10
_characters = Character.listFromJson(jsonList);
} else {
print("Error: Fail to connect the API.( statusCode : ${response.statusCode} )");
}
} catch (e) {
print("Exceptions error: ( ${e} )");
}
// print("//// DEBUG // _characters.length: ${_characters.length} - number of valid data");
// print("//// DEBUG // _characters:${_characters}");
return _characters;
}
}
......@@ -23,6 +23,7 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^1.0.0
# 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