From 6a2df6938ea8c10172411aff7d1a921c939fdcfc Mon Sep 17 00:00:00 2001 From: Supermjork Date: Sun, 31 Dec 2023 13:49:46 +0200 Subject: [PATCH] POST broke ;-; --- lib/arrival_post.dart | 46 +++++++++++++++++++++++++------------------ lib/main.dart | 6 +++++- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/arrival_post.dart b/lib/arrival_post.dart index 7b8abca..cdd3cbe 100644 --- a/lib/arrival_post.dart +++ b/lib/arrival_post.dart @@ -4,25 +4,33 @@ import 'package:http/http.dart' as http; import 'package:witl/arrival.dart'; Future insertArrival(String date, int weekday, int line, bool direction) async { - // POST response as JSON - final response = await http.post( - Uri.parse('http://141.144.238.26:48502/arrivals/insert'), - headers: { - // Headers used - 'Content-Type': 'application/json; charset=UTF-8', - }, - // Encoding a singular json entry - body: jsonEncode( { - 'time_of_day': date, - 'week_day': weekday, - 'tram_line': line, - 'direction': direction, - }) - ); + final client = http.Client(); - if (response.statusCode == 201) { - return Arrival.fromJson(jsonDecode(response.body) as Map); - } else { - throw Exception('Insertion Failure.'); + try { + // POST response as JSON + final response = await client.post( + Uri.parse('http://141.144.238.26:48502/arrivals/insert'), + headers: { + // Headers used + 'Content-Type': 'application/json; charset=UTF-8', + }, + // Encoding a singular json entry + body: jsonEncode( { + '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); + } else { + throw Exception('Insertion Failure.'); + } + } catch (error) { + throw Exception("Error Posting: $error"); + } finally { + client.close(); } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 52d91a3..e964e66 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,7 +12,8 @@ class MyApp extends StatefulWidget { @override State createState() => _MyAppState(); - static _MyAppState of(BuildContext context) => context.findAncestorStateOfType<_MyAppState>()!; + static _MyAppState of(BuildContext context) => + context.findAncestorStateOfType<_MyAppState>()!; } class _MyAppState extends State { @@ -67,10 +68,13 @@ class _MyAppState extends State { switch(newTheme) { case "Light": _theme = ThemeMode.light; + break; case "Dark": _theme = ThemeMode.dark; + break; case "System Default": _theme = ThemeMode.system; + break; } }); }