68 lines
2.2 KiB
Dart
68 lines
2.2 KiB
Dart
import 'package:cheminova/models/user_model.dart';
|
|
import 'package:cheminova/utils/api_urls.dart';
|
|
import 'package:cheminova/utils/common_api_service.dart';
|
|
import 'package:cheminova/utils/show_snackbar.dart';
|
|
|
|
class HomeService {
|
|
// Function to fetch user details from the API
|
|
Future<UserModel?> getUser({String? token}) async {
|
|
try {
|
|
// Making a GET request to fetch user profile data
|
|
final response = await commonApiService<UserModel>(
|
|
method: "GET",
|
|
url: ApiUrls.profileUrl,
|
|
additionalHeaders: { // Pass the token here
|
|
'Authorization': 'Bearer $token',
|
|
},
|
|
fromJson: (json) {
|
|
// Callback to parse the JSON response into a UserModel
|
|
if (json['user'] != null) {
|
|
// Parse the user data from the API response
|
|
return UserModel.fromJson(
|
|
json['user']); // Convert JSON to UserModel
|
|
}
|
|
return json as UserModel; // Return the response as UserModel if no 'user' key found
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
print(e.toString());
|
|
showSnackbar(e.toString()); // Optional: show error using a Snackbar
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Function to send FCM token to the server
|
|
Future<Map<String, dynamic>?> fcmToken(Map<String, dynamic> data,
|
|
String? token) async {
|
|
try {
|
|
final response = await commonApiService<String>(
|
|
// Making a POST request to send the FCM token
|
|
url: ApiUrls.fcmUrl,
|
|
method: 'POST',
|
|
body: data,
|
|
// Body data to be sent in the request
|
|
fromJson: (json) => json as String,
|
|
// Just return the string response
|
|
additionalHeaders: {
|
|
// Pass the token here
|
|
'Authorization': 'Bearer $token',
|
|
// Bearer token for authenticated requests
|
|
},
|
|
);
|
|
|
|
|
|
if (response != null) {
|
|
// Since the response is a string, wrap it in a Map to avoid type issues
|
|
return {
|
|
'message': response
|
|
}; // Return the response in a map with 'message' as the key
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
showSnackbar(e.toString()); // Handle any errors
|
|
return null;
|
|
}
|
|
}
|
|
}
|