You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/controllers/providers/api/departments_provider.dart

64 lines
1.8 KiB
Dart

3 years ago
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart';
3 years ago
import '../../../models/department.dart';
import '../../api_routes/urls.dart';
3 years ago
3 years ago
class DepartmentsProvider extends ChangeNotifier {
3 years ago
//reset provider data
3 years ago
void reset() {
3 years ago
departments = null;
stateCode = null;
}
// state code of current request to defied error message
// like 400 customer request failed
// 500 service not available
3 years ago
int? stateCode;
3 years ago
// contain user data
// when user not login or register _user = null
3 years ago
List<Department>? departments;
3 years ago
// when categories in-process _loading = true
// done _loading = true
// failed _loading = false
3 years ago
bool? isLoading;
3 years ago
/// return -2 if request in progress
/// return -1 if error happen when sending request
/// return state code if request complete may be 200, 404 or 403
/// for more details check http state manager
/// lib\controllers\http_status_manger\http_status_manger.dart
3 years ago
Future<int> getDepartment(String host) async {
if (isLoading == true) {
3 years ago
return -2;
3 years ago
}
3 years ago
isLoading = true;
notifyListeners();
Response response;
3 years ago
try {
3 years ago
response = await get(
3 years ago
Uri.parse(host + URLs.getDepartments),
headers: {"Content-Type": "application/json; charset=utf-8"},
3 years ago
);
3 years ago
} catch (error) {
3 years ago
isLoading = false;
stateCode = -1;
notifyListeners();
return -1;
}
stateCode = response.statusCode;
3 years ago
if (response.statusCode >= 200 && response.statusCode < 300) {
3 years ago
// client's request was successfully received
List listJson = json.decode(utf8.decode(response.bodyBytes).replaceAll("\\", ""));
departments = listJson.map((department) => Department.fromJson(department)).toList();
3 years ago
}
isLoading = false;
notifyListeners();
return response.statusCode;
}
3 years ago
}