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.
84 lines
2.3 KiB
Dart
84 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/controllers/api_routes/api_manager.dart';
|
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
|
import 'package:test_sa/models/new_models/building.dart';
|
|
|
|
class FmsBuildingProvider extends ChangeNotifier {
|
|
//reset provider data
|
|
void reset() {
|
|
_buildings = null;
|
|
_stateCode = null;
|
|
}
|
|
|
|
// state code of current request to defied error message
|
|
int? _stateCode;
|
|
int? get stateCode => _stateCode;
|
|
|
|
// contain buildings data
|
|
List<Building>? _buildings;
|
|
List<Building>? get buildings => _buildings;
|
|
|
|
// loading state
|
|
bool _loading = false;
|
|
bool get isLoading => _loading;
|
|
|
|
set isLoading(bool isLoading) {
|
|
_loading = isLoading;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Get buildings by site ID
|
|
Future<int> getBuildingsBySiteId(int siteId) async {
|
|
if (_loading == true) return -2;
|
|
_loading = true;
|
|
notifyListeners();
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.post(
|
|
URLs.getCustomerBuildings,
|
|
body: {
|
|
"siteId": siteId,
|
|
},
|
|
);
|
|
_stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// client's request was successfully received
|
|
List buildingsListJson = json.decode(response.body)["data"];
|
|
_buildings = buildingsListJson.map((building) => Building.fromJson(building)).toList();
|
|
}
|
|
_loading = false;
|
|
notifyListeners();
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
_loading = false;
|
|
_stateCode = -1;
|
|
notifyListeners();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/// Get buildings list directly without provider state
|
|
Future<List<Building>> getBuildingsListBySiteId(int siteId) async {
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.post(
|
|
URLs.getCustomerBuildings,
|
|
body: {
|
|
"siteId": siteId,
|
|
},
|
|
);
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
List buildingsListJson = json.decode(response.body)["data"];
|
|
return buildingsListJson.map((building) => Building.fromJson(building)).toList();
|
|
}
|
|
return [];
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
}
|
|
}
|