pd-android-app/lib/controller/notification_api_service.dart
2025-04-28 13:34:34 +05:30

49 lines
1.5 KiB
Dart

import 'package:dio/dio.dart';
import 'package:cheminova/models/notification_model.dart';
import '../utils/api_urls.dart';
import '../utils/app_interceptor.dart';
class NotificationService {
// final Dio _dio = Dio();
Future<List<NotificationModel>?> fetchNotifications(String token, String date) async {
final String url = ApiUrls.getNotificationUrl;
final dio = Dio();
dio.interceptors.add(AuthInterceptor());
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
}
}