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,8 +4,11 @@ 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 {
final client = http.Client();
try {
// POST response as JSON // POST response as JSON
final response = await http.post( final response = await client.post(
Uri.parse('http://141.144.238.26:48502/arrivals/insert'), Uri.parse('http://141.144.238.26:48502/arrivals/insert'),
headers: <String, String> { headers: <String, String> {
// Headers used // Headers used
@@ -18,11 +21,16 @@ Future<Arrival> insertArrival(String date, int weekday, int line, bool direction
'tram_line': line, 'tram_line': line,
'direction': direction, 'direction': direction,
}) })
); ).timeout(const Duration(seconds: 10));
if (response.statusCode == 201) { if (response.statusCode == 201) {
return Arrival.fromJson(jsonDecode(response.body) as Map<String, dynamic>); return Arrival.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
} else { } else {
throw Exception('Insertion Failure.'); 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;
} }
}); });
} }