POST broke ;-;

This commit is contained in:
2023-12-31 13:49:46 +02:00
parent 49508758b5
commit 6a2df6938e
2 changed files with 32 additions and 20 deletions

View File

@@ -4,25 +4,33 @@ import 'package:http/http.dart' as http;
import 'package:witl/arrival.dart'; import 'package:witl/arrival.dart';
Future<Arrival> insertArrival(String date, int weekday, int line, bool direction) async { Future<Arrival> insertArrival(String date, int weekday, int line, bool direction) async {
// POST response as JSON final client = http.Client();
final response = await http.post(
Uri.parse('http://141.144.238.26:48502/arrivals/insert'),
headers: <String, String> {
// Headers used
'Content-Type': 'application/json; charset=UTF-8',
},
// Encoding a singular json entry
body: jsonEncode(<String, dynamic> {
'time_of_day': date,
'week_day': weekday,
'tram_line': line,
'direction': direction,
})
);
if (response.statusCode == 201) { try {
return Arrival.fromJson(jsonDecode(response.body) as Map<String, dynamic>); // POST response as JSON
} else { final response = await client.post(
throw Exception('Insertion Failure.'); Uri.parse('http://141.144.238.26:48502/arrivals/insert'),
headers: <String, String> {
// Headers used
'Content-Type': 'application/json; charset=UTF-8',
},
// Encoding a singular json entry
body: jsonEncode(<String, dynamic> {
'time_of_day': date,
'week_day': weekday,
'tram_line': line,
'direction': direction,
})
).timeout(const Duration(seconds: 10));
if (response.statusCode == 201) {
return Arrival.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
} else {
throw Exception('Insertion Failure.');
}
} catch (error) {
throw Exception("Error Posting: $error");
} finally {
client.close();
} }
} }

View File

@@ -12,7 +12,8 @@ class MyApp extends StatefulWidget {
@override @override
State<MyApp> createState() => _MyAppState(); State<MyApp> createState() => _MyAppState();
static _MyAppState of(BuildContext context) => context.findAncestorStateOfType<_MyAppState>()!; static _MyAppState of(BuildContext context) =>
context.findAncestorStateOfType<_MyAppState>()!;
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
@@ -67,10 +68,13 @@ class _MyAppState extends State<MyApp> {
switch(newTheme) { switch(newTheme) {
case "Light": case "Light":
_theme = ThemeMode.light; _theme = ThemeMode.light;
break;
case "Dark": case "Dark":
_theme = ThemeMode.dark; _theme = ThemeMode.dark;
break;
case "System Default": case "System Default":
_theme = ThemeMode.system; _theme = ThemeMode.system;
break;
} }
}); });
} }