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 'package:http/http.dart' as http;
|
||||
|
||||
Future<Album> createAlbum(String title) async {
|
||||
@@ -1,10 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:witl/home_screen.dart';
|
||||
import 'package:witl/input_data.dart';
|
||||
import 'package:witl/arrival_fetch.dart';
|
||||
|
||||
class FetchAPI extends StatelessWidget {
|
||||
class FetchAPI extends StatefulWidget {
|
||||
const FetchAPI({super.key});
|
||||
|
||||
@override
|
||||
State<FetchAPI> createState() => _FetchAPIState();
|
||||
}
|
||||
|
||||
class _FetchAPIState extends State<FetchAPI> {
|
||||
late Future<List<Arrival>> fetchedArrivals;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@@ -14,33 +20,71 @@ class FetchAPI extends StatelessWidget {
|
||||
body: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
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(
|
||||
onTap:() {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => const HomeScreen())
|
||||
);
|
||||
onTap: () {
|
||||
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: const Text("To 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")
|
||||
child: const Text("Homescreen"),
|
||||
),
|
||||
),
|
||||
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});
|
||||
|
||||
@override
|
||||
_InputDataState createState() => _InputDataState();
|
||||
State<InputData> createState() => _InputDataState();
|
||||
}
|
||||
|
||||
class _InputDataState extends State<InputData> {
|
||||
|
||||
Reference in New Issue
Block a user