47 lines
1.4 KiB
Dart
47 lines
1.4 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:cheminova/models/notification_model.dart';
|
|
|
|
import '../utils/api_urls.dart';
|
|
|
|
class NotificationService {
|
|
final Dio _dio = Dio();
|
|
|
|
Future<List<NotificationModel>?> fetchNotifications(String token, String date) async {
|
|
final String url = ApiUrls.getNotificationUrl;
|
|
|
|
try {
|
|
final response = await _dio.get(
|
|
url,
|
|
options: Options(
|
|
headers: {
|
|
'Authorization': 'Bearer $token',
|
|
},
|
|
),
|
|
queryParameters: {'Date': date},
|
|
);
|
|
|
|
// Ensure the response is not void and contains expected data
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
final Map<String, dynamic> data = response.data as Map<String, dynamic>;
|
|
|
|
if (data.containsKey('success') && data['success'] == true) {
|
|
List notifications = data['notifications'] ?? [];
|
|
List<NotificationModel> notificationModels = notifications.map((notification) {
|
|
return NotificationModel.fromJson(notification);
|
|
}).toList();
|
|
|
|
return notificationModels; // Return the list of NotificationModel
|
|
} else {
|
|
print('Failed to fetch notifications: ${data['message'] ?? 'Unknown error'}');
|
|
}
|
|
} else {
|
|
print('Unexpected response: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
print('Exception occurred: $e');
|
|
}
|
|
|
|
return null; // Return null if fetching fails
|
|
}
|
|
}
|