embty tile
This commit is contained in:
@@ -4,32 +4,31 @@ import 'dart:async';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
Future<List<Arrival>> fetchArrivals() async {
|
Future<List<Arrival>> fetchArrivals() async {
|
||||||
final response = await http
|
try {
|
||||||
.get(Uri.parse('http://141.144.238.26:48502/arrivals/all'));
|
final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all'));
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
// If the server did return a 200 OK response,
|
List<dynamic> data = jsonDecode(response.body);
|
||||||
// then parse the JSON.
|
List<Arrival> arrivals = List<Arrival>.from(data.map((dynamic arrivalJson) {
|
||||||
List<dynamic> data = jsonDecode(response.body);
|
return Arrival.fromJson(arrivalJson);
|
||||||
List<Arrival> posts = List<Arrival>.from(data.map((dynamic postJson) {
|
}));
|
||||||
return Arrival.fromJson(postJson);
|
return arrivals;
|
||||||
}));
|
} else {
|
||||||
} else {
|
throw Exception('Failed to load Arrival');
|
||||||
// If the server did not return a 200 OK response,
|
}
|
||||||
// then throw an exception.
|
} catch (error) {
|
||||||
throw Exception('Failed to load Arrival');
|
throw Exception('Failed to fetch data: $error');
|
||||||
}
|
}
|
||||||
|
|
||||||
throw Exception("API Unavailable");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Arrival {
|
class Arrival {
|
||||||
final String timeOfDay;
|
final String timeOfDay;
|
||||||
final int weekDay;
|
final int weekDay;
|
||||||
final int tramLine;
|
final int tramLine;
|
||||||
final bool direction;
|
final bool direction;
|
||||||
|
|
||||||
const Arrival({
|
Arrival({
|
||||||
required this.timeOfDay,
|
required this.timeOfDay,
|
||||||
required this.weekDay,
|
required this.weekDay,
|
||||||
required this.tramLine,
|
required this.tramLine,
|
||||||
@@ -37,21 +36,12 @@ class Arrival {
|
|||||||
});
|
});
|
||||||
|
|
||||||
factory Arrival.fromJson(Map<String, dynamic> json) {
|
factory Arrival.fromJson(Map<String, dynamic> json) {
|
||||||
return switch (json) {
|
return Arrival(
|
||||||
{
|
timeOfDay: json['time_of_day'] as String,
|
||||||
'time_of_day': String jsonTime,
|
weekDay: json['week_day'] as int,
|
||||||
'week_day': int jsonDay,
|
tramLine: json['tram_line'] as int,
|
||||||
'tram_line': int jsonTramLine,
|
direction: json['direction'] as bool,
|
||||||
'direction': bool jsonDirec,
|
);
|
||||||
} =>
|
|
||||||
Arrival(
|
|
||||||
// Class : json declaration
|
|
||||||
timeOfDay: jsonTime,
|
|
||||||
weekDay: jsonDay,
|
|
||||||
tramLine: jsonTramLine,
|
|
||||||
direction: jsonDirec,
|
|
||||||
),
|
|
||||||
_ => throw const FormatException('Failed to load Arrival.'),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user