diff --git a/frontend/lib/notifications/notification_list.dart b/frontend/lib/notifications/notification_list.dart index a1c1ad3..e38055f 100644 --- a/frontend/lib/notifications/notification_list.dart +++ b/frontend/lib/notifications/notification_list.dart @@ -9,7 +9,7 @@ class NotificationList extends StatefulWidget { const NotificationList({super.key}); @override - _NotificationListState createState() => _NotificationListState(); + State createState() => _NotificationListState(); } class _NotificationListState extends State { @@ -61,6 +61,7 @@ class _NotificationListState extends State { return false; } await BackendAPI.instance.markNotificationAsRead(item.id); + if (!context.mounted) return false; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Event marked as read'), @@ -95,6 +96,7 @@ class _NotificationListState extends State { return false; } await BackendAPI.instance.markNotificationAsNew(item.id); + if (!context.mounted) return false; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Event marked as unread'), diff --git a/frontend/lib/settings/settings.dart b/frontend/lib/settings/settings.dart index e0bf73f..dfc271e 100644 --- a/frontend/lib/settings/settings.dart +++ b/frontend/lib/settings/settings.dart @@ -12,7 +12,7 @@ class Settings extends StatefulWidget { const Settings({super.key}); @override - _SettingsState createState() => _SettingsState(); + State createState() => _SettingsState(); } class _SettingsState extends State { @@ -125,7 +125,7 @@ class _SettingsState extends State { SizedBox(height: 16), // Theme selector DropdownButtonFormField( - value: _selectedTheme, + initialValue: _selectedTheme, decoration: const InputDecoration( border: UnderlineInputBorder(), labelText: 'Theme', @@ -162,10 +162,12 @@ class _SettingsState extends State { _apiKeyController.text, ); + if (!context.mounted) return; setState(() { _testOk = testResult == null; - if (testResult == null) + if (testResult == null) { _connectionModified = false; + } }); if (testResult == null) { diff --git a/frontend/lib/utils/friendly_date_formatter.dart b/frontend/lib/utils/friendly_date_formatter.dart index 3d187eb..8a9ba01 100644 --- a/frontend/lib/utils/friendly_date_formatter.dart +++ b/frontend/lib/utils/friendly_date_formatter.dart @@ -15,10 +15,12 @@ class FriendlyDateFormatter { ); if (timeSince.inMinutes < 60) return '${timeSince.inMinutes} minutes ago'; - if (dateTimeWithoutTime == today) + if (dateTimeWithoutTime == today) { return 'Today at ${DateFormat.Hm().format(dateTime)}'; - if (dateTimeWithoutTime == yesterday) + } + if (dateTimeWithoutTime == yesterday) { return 'Yesterday at ${DateFormat.Hm().format(dateTime)}'; + } return DateFormat.yMMMMd().add_Hm().format(dateTime); } diff --git a/frontend/lib/utils/oott_api.dart b/frontend/lib/utils/oott_api.dart index cf9a830..df69cef 100644 --- a/frontend/lib/utils/oott_api.dart +++ b/frontend/lib/utils/oott_api.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:encrypter/encrypter/xor.dart'; +import 'package:flutter/foundation.dart'; import 'package:frontend/utils/pref_utils.dart'; import '../model/notification.dart'; @@ -16,8 +17,8 @@ class BackendAPI { PrefUtil.getValue("base_url", "http://localhost:3000/api") as String; _apiKey = XOR().xorDecode(PrefUtil.getValue("api_key", "") as String); - print('Base URL: $_baseUrl'); - print('API KEY $_apiKey'); + debugPrint('Base URL: $_baseUrl'); + debugPrint('API KEY: $_apiKey'); _dio = Dio( BaseOptions( @@ -32,7 +33,7 @@ class BackendAPI { // Returns null if the test was successful, and a String with a message about the issue if not static Future test(String baseUrl, String apiKey) async { - print('About to test API with baseUrl=$baseUrl and apiKey=$apiKey'); + debugPrint('About to test API with baseUrl=$baseUrl and apiKey=$apiKey'); Dio dio = Dio( BaseOptions( baseUrl: baseUrl, @@ -67,22 +68,22 @@ class BackendAPI { late Dio _dio; Future markNotificationAsRead(int id) async { - print('About to call /notifications/$id'); + debugPrint('About to call /notifications/$id'); await _dio.get('/notifications/$id'); } Future markNotificationAsNew(int id) async { - print('About to call /notifications/$id/mark_as_new'); + debugPrint('About to call /notifications/$id/mark_as_new'); await _dio.post('/notifications/$id/mark_as_new'); } Future markAllNotificationsAsRead() async { - print('About to call /notifications/mark_all_as_old'); + debugPrint('About to call /notifications/mark_all_as_old'); await _dio.post('/notifications/mark_all_as_old'); } Future> listNotifications(bool? isNew, int offset) async { - print('About to call /notifications'); + debugPrint('About to call /notifications'); Response response; response = await _dio.get( @@ -94,16 +95,16 @@ class BackendAPI { }, ); - print('Received: ' + response.data.toString()); + debugPrint('Received: ${response.data}'); List list = response.data; - print('List contains ' + list.length.toString() + ' items'); + debugPrint('List contains ${list.length} items'); List events = List.from( list.map((item) => Notification.fromJson(item)), ); - print('Parsed ' + events.length.toString() + ' events'); + debugPrint('Parsed ${events.length} events'); return events; } diff --git a/frontend/lib/utils/pref_utils.dart b/frontend/lib/utils/pref_utils.dart index 97b6d24..88895c8 100644 --- a/frontend/lib/utils/pref_utils.dart +++ b/frontend/lib/utils/pref_utils.dart @@ -11,27 +11,24 @@ class PrefUtil { } static void setValue(String key, Object value) { - switch (value.runtimeType) { - case String: - preferences.setString(key, value as String); - break; - case bool: - preferences.setBool(key, value as bool); - break; - case int: - preferences.setInt(key, value as int); - break; + switch (value) { + case String s: + preferences.setString(key, s); + case bool b: + preferences.setBool(key, b); + case int i: + preferences.setInt(key, i); default: } } static Object getValue(String key, Object defaultValue) { - switch (defaultValue.runtimeType) { - case String: + switch (defaultValue) { + case String _: return preferences.getString(key) ?? defaultValue; - case bool: + case bool _: return preferences.getBool(key) ?? defaultValue; - case int: + case int _: return preferences.getInt(key) ?? defaultValue; default: return defaultValue;