57 lines
2.2 KiB
Dart
57 lines
2.2 KiB
Dart
import 'package:cheminova/models/select_task_response.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../services/api_client.dart';
|
|
import '../services/api_urls.dart';
|
|
|
|
// Provider class responsible for managing tasks and fetching task data from the API
|
|
class SelectTaskProvider extends ChangeNotifier {
|
|
|
|
// Constructor that automatically fetches tasks when the provider is initialized
|
|
SelectTaskProvider() {
|
|
getTask(); // Fetch tasks upon provider creation
|
|
}
|
|
|
|
final _apiClient = ApiClient(); // API client instance for handling HTTP requests
|
|
List<Tasks> tasksList = []; // List to store tasks fetched from the API
|
|
|
|
bool _isLoading = false; // Flag to track the loading state (whether data is being loaded)
|
|
|
|
bool get isLoading => _isLoading; // Getter for the loading state
|
|
|
|
// Function to set loading state and notify listeners to update UI
|
|
void setLoading(bool loading) {
|
|
_isLoading = loading;
|
|
notifyListeners(); // Notifies listeners that the loading state has changed
|
|
}
|
|
|
|
// Function to fetch tasks from the API
|
|
Future<void> getTask() async {
|
|
setLoading(true); // Set loading to true when data fetching starts
|
|
try {
|
|
// Send GET request to the API to retrieve tasks
|
|
Response response = await _apiClient.get(ApiUrls.kycSelectTaskUrl);
|
|
setLoading(false); // Set loading to false after data has been fetched
|
|
|
|
// If the response is successful (status code 200), process the data
|
|
if (response.statusCode == 200) {
|
|
// Parse the response data into a SelectTaskKycResponse object
|
|
final data = SelectTaskKycResponse.fromJson(response.data);
|
|
|
|
// Filter tasks to include only those that are not completed
|
|
tasksList = data!.tasks!
|
|
.where((element) => element.taskStatus!.toLowerCase() != "completed")
|
|
.toList();
|
|
notifyListeners(); // Notify listeners to update the UI with the new tasks
|
|
}
|
|
} catch (e) {
|
|
setLoading(false); // Set loading to false if an error occurs
|
|
if (kDebugMode) {
|
|
print("Error occurred while fetching tasks: $e"); // Log the error in debug mode
|
|
}
|
|
}
|
|
}
|
|
}
|