DAMN FORMATTING
This commit is contained in:
@@ -1,53 +1,57 @@
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
Future<FetchAlbum> fetchArrivals() async {
|
Future<List<Arrival>> fetchArrivals() async {
|
||||||
final response = await http
|
final response = await http
|
||||||
.get(Uri.parse(DB_URL));
|
.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,
|
// If the server did return a 200 OK response,
|
||||||
// then parse the JSON.
|
// then parse the JSON.
|
||||||
return FetchAlbum.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
|
List<dynamic> data = jsonDecode(response.body);
|
||||||
|
List<Arrival> posts = List<Arrival>.from(data.map((dynamic postJson) {
|
||||||
|
return Arrival.fromJson(postJson);
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
// If the server did not return a 200 OK response,
|
// If the server did not return a 200 OK response,
|
||||||
// then throw an exception.
|
// then throw an exception.
|
||||||
throw Exception('Failed to load album');
|
throw Exception('Failed to load Arrival');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw Exception("API Unavailable");
|
||||||
}
|
}
|
||||||
|
|
||||||
class FetchAlbum {
|
class Arrival {
|
||||||
final String timeOfDay;
|
final String timeOfDay;
|
||||||
final int weekDay;
|
final int weekDay;
|
||||||
final String tramLine;
|
final int tramLine;
|
||||||
final bool direction;
|
final bool direction;
|
||||||
|
|
||||||
const FetchAlbum({
|
const Arrival({
|
||||||
required this.timeOfDay,
|
required this.timeOfDay,
|
||||||
required this.weekDay,
|
required this.weekDay,
|
||||||
required this.tramLine,
|
required this.tramLine,
|
||||||
required this.direction,
|
required this.direction,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory FetchAlbum.fromJson(Map<String, dynamic> json) {
|
factory Arrival.fromJson(Map<String, dynamic> json) {
|
||||||
return switch (json) {
|
return switch (json) {
|
||||||
{
|
{
|
||||||
'time_of_day': String jsonTime,
|
'time_of_day': String jsonTime,
|
||||||
'week_day': int jsonDay,
|
'week_day': int jsonDay,
|
||||||
'tram_line': String jsonTramLine,
|
'tram_line': int jsonTramLine,
|
||||||
'direction': bool jsonDirec,
|
'direction': bool jsonDirec,
|
||||||
} =>
|
} =>
|
||||||
FetchAlbum(
|
Arrival(
|
||||||
// Class : json declaration
|
// Class : json declaration
|
||||||
timeOfDay: jsonTime,
|
timeOfDay: jsonTime,
|
||||||
weekDay: jsonDay,
|
weekDay: jsonDay,
|
||||||
tramLine: jsonTramLine,
|
tramLine: jsonTramLine,
|
||||||
direction: jsonDirec,
|
direction: jsonDirec,
|
||||||
),
|
),
|
||||||
_ => throw const FormatException('Failed to load album.'),
|
_ => throw const FormatException('Failed to load Arrival.'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,19 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:witl/album_fetch.dart';
|
import 'package:witl/album_fetch.dart';
|
||||||
|
|
||||||
class FetchAPI extends StatefulWidget {
|
class FetchAPI extends StatefulWidget {
|
||||||
const FetchAPI({super.key});
|
const FetchAPI({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<FetchAPI> createState() => _FetchAPIState();
|
State<FetchAPI> createState() => _FetchAPIState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _FetchAPIState extends State<FetchAPI> {
|
class _FetchAPIState extends State<FetchAPI> {
|
||||||
late Future<FetchAlbum> fetchedAlbum;
|
late Future<List<Arrival>> fetchedArrivals;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
fetchedAlbum = fetchArrivals();
|
fetchedArrivals = fetchArrivals();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -25,29 +25,34 @@ class _FetchAPIState extends State<FetchAPI> {
|
|||||||
),
|
),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
children: [
|
children: [
|
||||||
FutureBuilder<FetchAlbum> (
|
FutureBuilder<List<Arrival>>(
|
||||||
future: fetchedAlbum,
|
future: fetchedArrivals,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
return Text(snapshot.data!.timeOfDay);
|
return const CircularProgressIndicator();
|
||||||
} else if (snapshot.hasError) {
|
} else if (snapshot.hasError) {
|
||||||
return Text('${snapshot.error}');
|
return Text('Failed to fetch data.\nError: ${snapshot.error}');
|
||||||
|
} else {
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: snapshot.data!.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return ListTile(
|
||||||
|
title: Text(snapshot.data![index].timeOfDay),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return const CircularProgressIndicator();
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
InkWell(
|
InkWell(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.pushNamedAndRemoveUntil(
|
Navigator.pushNamedAndRemoveUntil(
|
||||||
context,
|
context, '/', (route) => false);
|
||||||
'/',
|
|
||||||
(route) => false
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
child: Container(
|
child: Container(
|
||||||
padding: const EdgeInsets.all(20.0),
|
padding: const EdgeInsets.all(20.0),
|
||||||
child: const Text("Homescreen")),
|
child: const Text("Homescreen"),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user