49
lib/arrival_fetch.dart
Normal file
49
lib/arrival_fetch.dart
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
Future<List<Arrival>> fetchArrivals() async {
|
||||||
|
try {
|
||||||
|
final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all'));
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
// List<dynamic> data = jsonDecode(response.body);
|
||||||
|
// List<Arrival> arrivals = List<Arrival>.from(data.map((dynamic arrivalJson) {
|
||||||
|
// return Arrival.fromJson(arrivalJson);
|
||||||
|
// }));
|
||||||
|
Iterable I = json.decode(response.body);
|
||||||
|
List<Arrival> arrivals = List<Arrival>.from(I.map((model)=>Arrival.fromJson(model)));
|
||||||
|
return arrivals;
|
||||||
|
} else {
|
||||||
|
throw Exception('Failed to load Arrival');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw Exception('Failed to fetch data: $error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Arrival {
|
||||||
|
final String timeOfDay;
|
||||||
|
final int weekDay;
|
||||||
|
final int tramLine;
|
||||||
|
final bool direction;
|
||||||
|
|
||||||
|
Arrival({
|
||||||
|
required this.timeOfDay,
|
||||||
|
required this.weekDay,
|
||||||
|
required this.tramLine,
|
||||||
|
required this.direction,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory Arrival.fromJson(Map<String, dynamic> json) {
|
||||||
|
return Arrival(
|
||||||
|
timeOfDay: json['time_of_day'] as String,
|
||||||
|
weekDay: json['week_day'] as int,
|
||||||
|
tramLine: json['tram_line'] as int,
|
||||||
|
direction: json['direction'] as bool,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
Future<Album> createAlbum(String title) async {
|
Future<Album> createAlbum(String title) async {
|
||||||
@@ -1,10 +1,16 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:witl/home_screen.dart';
|
import 'package:witl/arrival_fetch.dart';
|
||||||
import 'package:witl/input_data.dart';
|
|
||||||
|
|
||||||
class FetchAPI extends StatelessWidget {
|
class FetchAPI extends StatefulWidget {
|
||||||
const FetchAPI({super.key});
|
const FetchAPI({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<FetchAPI> createState() => _FetchAPIState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _FetchAPIState extends State<FetchAPI> {
|
||||||
|
late Future<List<Arrival>> fetchedArrivals;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -14,33 +20,71 @@ class FetchAPI extends StatelessWidget {
|
|||||||
body: Column(
|
body: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: SizedBox(
|
||||||
|
child: FutureBuilder<List<Arrival>>(
|
||||||
|
future: fetchedArrivals,
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
return const CircularProgressIndicator();
|
||||||
|
} else if (snapshot.hasError) {
|
||||||
|
return Text('Failed to fetch data.\nError: ${snapshot.error}');
|
||||||
|
} else {
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: snapshot.data!.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return ListTile(
|
||||||
|
title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"),
|
||||||
|
titleAlignment: ListTileTitleAlignment.center,
|
||||||
|
subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 30,),
|
||||||
InkWell(
|
InkWell(
|
||||||
onTap:() {
|
onTap: () {
|
||||||
Navigator.push(
|
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
|
||||||
context,
|
|
||||||
MaterialPageRoute(builder: (context) => const HomeScreen())
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
child: Container(
|
child: Container(
|
||||||
padding: const EdgeInsets.all(20.0),
|
padding: const EdgeInsets.all(20.0),
|
||||||
child: const Text("To Homescreen")
|
child: const Text("Homescreen"),
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 20),
|
|
||||||
InkWell(
|
|
||||||
onTap:() {
|
|
||||||
Navigator.push(
|
|
||||||
context,
|
|
||||||
MaterialPageRoute(builder: (context) => const InputData())
|
|
||||||
);
|
|
||||||
},
|
|
||||||
child: Container(
|
|
||||||
padding: const EdgeInsets.all(20.0),
|
|
||||||
child: const Text("To Input Data")
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
const SizedBox(height: 35,),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
fetchedArrivals = fetchArrivals();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String weekNumToString(int weekday) {
|
||||||
|
switch (weekday) {
|
||||||
|
case 1:
|
||||||
|
return "Monday";
|
||||||
|
case 2:
|
||||||
|
return "Tuesday";
|
||||||
|
case 3:
|
||||||
|
return "Wednesday";
|
||||||
|
case 4:
|
||||||
|
return "Thursday";
|
||||||
|
case 5:
|
||||||
|
return "Friday";
|
||||||
|
case 6:
|
||||||
|
return "Saturday";
|
||||||
|
case 7:
|
||||||
|
return "Sunday";
|
||||||
|
}
|
||||||
|
|
||||||
|
throw "AAAAAAAAA";
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ class InputData extends StatefulWidget {
|
|||||||
const InputData({super.key});
|
const InputData({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_InputDataState createState() => _InputDataState();
|
State<InputData> createState() => _InputDataState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _InputDataState extends State<InputData> {
|
class _InputDataState extends State<InputData> {
|
||||||
|
|||||||
Reference in New Issue
Block a user