Update
Logging data Try-catch function
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import '../product/services/api_services.dart';
|
||||
import '../product/services/language_services.dart';
|
||||
import '../product/shared/model/ward_model.dart';
|
||||
import '../product/shared/shared_snack_bar.dart';
|
||||
import '../product/utils/response_status_utils.dart';
|
||||
|
||||
import '../product/shared/model/district_model.dart';
|
||||
import '../product/shared/model/province_model.dart';
|
||||
import '../feature/devices/device_model.dart';
|
||||
@@ -80,56 +80,42 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
List<DropdownMenuItem<Province>> provincesData = [];
|
||||
provincesData.clear();
|
||||
sinkListProvinces.add(provincesData);
|
||||
try {
|
||||
List<Province> provinces = await apiServices.getAllProvinces();
|
||||
await apiServices.execute(context, () async {
|
||||
List<Province> provinces = await apiServices.getAllProvinces();
|
||||
for (var province in provinces) {
|
||||
provincesData.add(
|
||||
DropdownMenuItem(value: province, child: Text(province.fullName!)));
|
||||
}
|
||||
sinkListProvinces.add(provincesData);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> getAllDistricts(BuildContext context, String provinceID) async {
|
||||
List<DropdownMenuItem<District>> districtsData = [];
|
||||
districtsData.clear();
|
||||
sinkListDistricts.add(districtsData);
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
final districts = await apiServices.getAllDistricts(provinceID);
|
||||
for (var district in districts) {
|
||||
districtsData.add(
|
||||
DropdownMenuItem(value: district, child: Text(district.fullName!)));
|
||||
}
|
||||
sinkListDistricts.add(districtsData);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> getAllWards(BuildContext context, String districtID) async {
|
||||
List<DropdownMenuItem<Ward>> wardsData = [];
|
||||
wardsData.clear();
|
||||
sinkListWards.add(wardsData);
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
final wards = await apiServices.getAllWards(districtID);
|
||||
for (var ward in wards) {
|
||||
wardsData.add(DropdownMenuItem(value: ward, child: Text(ward.fullName!)));
|
||||
wardsData
|
||||
.add(DropdownMenuItem(value: ward, child: Text(ward.fullName!)));
|
||||
}
|
||||
sinkListWards.add(wardsData);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> getDeviceInformation(
|
||||
@@ -138,7 +124,7 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
TextEditingController deviceNameController,
|
||||
TextEditingController latitudeController,
|
||||
TextEditingController longitudeController) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
Device device = await apiServices.getDeviceInformation(thingID);
|
||||
sinkDeviceInfo.add(device);
|
||||
deviceNameController.text = device.name ?? "";
|
||||
@@ -155,23 +141,24 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
String wardCode = areaPath[2];
|
||||
|
||||
// Kiểm tra các mã có hợp lệ không (không rỗng)
|
||||
if (provinceCode.isNotEmpty && districtCode.isNotEmpty && wardCode.isNotEmpty) {
|
||||
if (provinceCode.isNotEmpty &&
|
||||
districtCode.isNotEmpty &&
|
||||
wardCode.isNotEmpty) {
|
||||
try {
|
||||
// Lấy danh sách districts và wards
|
||||
await getAllDistricts(context,provinceCode);
|
||||
await getAllWards(context,districtCode);
|
||||
await getAllDistricts(context, provinceCode);
|
||||
await getAllWards(context, districtCode);
|
||||
|
||||
// Xử lý Province
|
||||
try {
|
||||
Province province = await apiServices.getProvinceByID(provinceCode);
|
||||
Map<String, String> provinceData = {
|
||||
"name": province.fullName ?? "Unknown Province",
|
||||
"code": province.code ?? provinceCode
|
||||
};
|
||||
sinkProvinceData.add(provinceData);
|
||||
|
||||
Province province =
|
||||
await apiServices.getProvinceByID(provinceCode);
|
||||
Map<String, String> provinceData = {
|
||||
"name": province.fullName ?? "Unknown Province",
|
||||
"code": province.code ?? provinceCode
|
||||
};
|
||||
sinkProvinceData.add(provinceData);
|
||||
} catch (e) {
|
||||
|
||||
// Thêm dữ liệu mặc định khi lỗi
|
||||
Map<String, String> provinceData = {
|
||||
"name": "Error Loading Province",
|
||||
@@ -179,20 +166,20 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
};
|
||||
sinkProvinceData.add(provinceData);
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
showErrorTopSnackBarCustom(context, e.toString());
|
||||
}
|
||||
|
||||
// Xử lý District
|
||||
try {
|
||||
District district = await apiServices.getDistrictByID(districtCode);
|
||||
Map<String, String> districData = {
|
||||
"name": district.fullName ?? "Unknown District",
|
||||
"code": district.code ?? districtCode,
|
||||
};
|
||||
sinkDistrictData.add(districData);
|
||||
District district =
|
||||
await apiServices.getDistrictByID(districtCode);
|
||||
Map<String, String> districData = {
|
||||
"name": district.fullName ?? "Unknown District",
|
||||
"code": district.code ?? districtCode,
|
||||
};
|
||||
sinkDistrictData.add(districData);
|
||||
} catch (e) {
|
||||
print("Lỗi khi lấy thông tin district ($districtCode): $e");
|
||||
log("Lỗi khi lấy thông tin district ($districtCode): $e");
|
||||
Map<String, String> districData = {
|
||||
"name": "Error Loading District",
|
||||
"code": districtCode,
|
||||
@@ -202,84 +189,168 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
|
||||
// Xử lý Ward
|
||||
try {
|
||||
Ward ward = await apiServices.getWardByID(wardCode);
|
||||
Map<String, String> wardMap = {
|
||||
"name": ward.fullName ?? "Unknown Ward",
|
||||
"code": ward.code ?? wardCode,
|
||||
};
|
||||
sinkWardData.add(wardMap);
|
||||
Ward ward = await apiServices.getWardByID(wardCode);
|
||||
Map<String, String> wardMap = {
|
||||
"name": ward.fullName ?? "Unknown Ward",
|
||||
"code": ward.code ?? wardCode,
|
||||
};
|
||||
sinkWardData.add(wardMap);
|
||||
} catch (e) {
|
||||
print("Lỗi khi lấy thông tin ward ($wardCode): $e");
|
||||
log("Lỗi khi lấy thông tin ward ($wardCode): $e");
|
||||
Map<String, String> wardMap = {
|
||||
"name": "Error Loading Ward",
|
||||
"code": wardCode,
|
||||
};
|
||||
sinkWardData.add(wardMap);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
print("Lỗi khi gọi getAllDistricts hoặc getAllWards: $e");
|
||||
log("Lỗi khi gọi getAllDistricts hoặc getAllWards: $e");
|
||||
}
|
||||
} else {
|
||||
await getAllProvinces(context);
|
||||
print("Một hoặc nhiều mã địa phương trống: Province: $provinceCode, District: $districtCode, Ward: $wardCode");
|
||||
log("Một hoặc nhiều mã địa phương trống: Province: $provinceCode, District: $districtCode, Ward: $wardCode");
|
||||
}
|
||||
} else {
|
||||
showNoIconTopSnackBar(context,"AreaPath không đủ thông tin: ${device.areaPath}",Colors.orangeAccent, Colors.white);
|
||||
showNoIconTopSnackBar(
|
||||
context,
|
||||
"AreaPath không đủ thông tin: ${device.areaPath}",
|
||||
Colors.orangeAccent,
|
||||
Colors.white);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
showNoIconTopSnackBar(context,"Lỗi trong getDeviceInfomation: $e",Colors.orangeAccent, Colors.white);
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// Device device = await apiServices.getDeviceInformation(thingID);
|
||||
// sinkDeviceInfo.add(device);
|
||||
// deviceNameController.text = device.name ?? "";
|
||||
// latitudeController.text = device.settings!.latitude ?? "";
|
||||
// longitudeController.text = device.settings!.longitude ?? "";
|
||||
|
||||
// if (device.areaPath != null && device.areaPath!.isNotEmpty) {
|
||||
// List<String> areaPath = device.areaPath!.split('_');
|
||||
|
||||
// // Kiểm tra độ dài của areaPath
|
||||
// if (areaPath.length >= 3) {
|
||||
// String provinceCode = areaPath[0];
|
||||
// String districtCode = areaPath[1];
|
||||
// String wardCode = areaPath[2];
|
||||
|
||||
// // Kiểm tra các mã có hợp lệ không (không rỗng)
|
||||
// if (provinceCode.isNotEmpty &&
|
||||
// districtCode.isNotEmpty &&
|
||||
// wardCode.isNotEmpty) {
|
||||
// try {
|
||||
// // Lấy danh sách districts và wards
|
||||
// await getAllDistricts(context, provinceCode);
|
||||
// await getAllWards(context, districtCode);
|
||||
|
||||
// // Xử lý Province
|
||||
// try {
|
||||
// Province province =
|
||||
// await apiServices.getProvinceByID(provinceCode);
|
||||
// Map<String, String> provinceData = {
|
||||
// "name": province.fullName ?? "Unknown Province",
|
||||
// "code": province.code ?? provinceCode
|
||||
// };
|
||||
// sinkProvinceData.add(provinceData);
|
||||
// } catch (e) {
|
||||
// // Thêm dữ liệu mặc định khi lỗi
|
||||
// Map<String, String> provinceData = {
|
||||
// "name": "Error Loading Province",
|
||||
// "code": provinceCode
|
||||
// };
|
||||
// sinkProvinceData.add(provinceData);
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
|
||||
// // Xử lý District
|
||||
// try {
|
||||
// District district =
|
||||
// await apiServices.getDistrictByID(districtCode);
|
||||
// Map<String, String> districData = {
|
||||
// "name": district.fullName ?? "Unknown District",
|
||||
// "code": district.code ?? districtCode,
|
||||
// };
|
||||
// sinkDistrictData.add(districData);
|
||||
// } catch (e) {
|
||||
// log("Lỗi khi lấy thông tin district ($districtCode): $e");
|
||||
// Map<String, String> districData = {
|
||||
// "name": "Error Loading District",
|
||||
// "code": districtCode,
|
||||
// };
|
||||
// sinkDistrictData.add(districData);
|
||||
// }
|
||||
|
||||
// // Xử lý Ward
|
||||
// try {
|
||||
// Ward ward = await apiServices.getWardByID(wardCode);
|
||||
// Map<String, String> wardMap = {
|
||||
// "name": ward.fullName ?? "Unknown Ward",
|
||||
// "code": ward.code ?? wardCode,
|
||||
// };
|
||||
// sinkWardData.add(wardMap);
|
||||
// } catch (e) {
|
||||
// print("Lỗi khi lấy thông tin ward ($wardCode): $e");
|
||||
// Map<String, String> wardMap = {
|
||||
// "name": "Error Loading Ward",
|
||||
// "code": wardCode,
|
||||
// };
|
||||
// sinkWardData.add(wardMap);
|
||||
// }
|
||||
// } catch (e) {
|
||||
// print("Lỗi khi gọi getAllDistricts hoặc getAllWards: $e");
|
||||
// }
|
||||
// } else {
|
||||
// await getAllProvinces(context);
|
||||
// print(
|
||||
// "Một hoặc nhiều mã địa phương trống: Province: $provinceCode, District: $districtCode, Ward: $wardCode");
|
||||
// }
|
||||
// } else {
|
||||
// showNoIconTopSnackBar(
|
||||
// context,
|
||||
// "AreaPath không đủ thông tin: ${device.areaPath}",
|
||||
// Colors.orangeAccent,
|
||||
// Colors.white);
|
||||
// }
|
||||
// }
|
||||
// } catch (e) {
|
||||
// showNoIconTopSnackBar(context, "Lỗi trong getDeviceInfomation: $e",
|
||||
// Colors.orangeAccent, Colors.white);
|
||||
// }
|
||||
}
|
||||
|
||||
Future<Province> getProvinceByName(BuildContext context, String name) async {
|
||||
try {
|
||||
return await apiServices.execute(context, () async {
|
||||
List<Province> provinces = await apiServices.getProvincesByName(name);
|
||||
if (provinces.isNotEmpty) {
|
||||
return provinces[0];
|
||||
} else {
|
||||
return Province(name: "null");
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showErrorTopSnackBarCustom(context, e.toString());
|
||||
}
|
||||
return Province(name: "null");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Future<District> getDistrictByName(
|
||||
BuildContext context, String name, String provinceCode) async {
|
||||
try {
|
||||
return apiServices.execute(context, () async {
|
||||
final districts = await apiServices.getDistrictsByName(name);
|
||||
return districts.firstWhere(
|
||||
(district) => district.provinceCode == provinceCode,
|
||||
(district) => district.provinceCode == provinceCode,
|
||||
orElse: () => District(name: "null"),
|
||||
);
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showErrorTopSnackBarCustom(context, e.toString());
|
||||
}
|
||||
return District(name: "null");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<Ward> getWardByName(
|
||||
BuildContext context, String name, String districtCode) async {
|
||||
try {
|
||||
return apiServices.execute(context, () async {
|
||||
final wards = await apiServices.getWardsByName(name);
|
||||
return wards.firstWhere(
|
||||
(ward) => ward.districtCode == districtCode,
|
||||
(ward) => ward.districtCode == districtCode,
|
||||
orElse: () => Ward(name: "null"),
|
||||
);
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showErrorTopSnackBarCustom(context, e.toString());
|
||||
}
|
||||
return Ward(name: "null");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateDevice(
|
||||
@@ -292,10 +363,10 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
String districtCode,
|
||||
String wardCode,
|
||||
) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
DateTime dateTime = DateTime.now();
|
||||
String formattedDateTime =
|
||||
DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
|
||||
DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
|
||||
Map<String, dynamic> body = {
|
||||
"name": name,
|
||||
"area_province": provinceCode,
|
||||
@@ -312,11 +383,30 @@ class DeviceUpdateBloc extends BlocBase {
|
||||
appLocalization(context).notification_update_device_success,
|
||||
appLocalization(context).notification_update_device_failed,
|
||||
);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
|
||||
});
|
||||
// try {
|
||||
// DateTime dateTime = DateTime.now();
|
||||
// String formattedDateTime =
|
||||
// DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
|
||||
// Map<String, dynamic> body = {
|
||||
// "name": name,
|
||||
// "area_province": provinceCode,
|
||||
// "area_district": districtCode,
|
||||
// "area_ward": wardCode,
|
||||
// "latitude": latitude,
|
||||
// "longitude": longitude,
|
||||
// "note": "User updated device infomation at $formattedDateTime",
|
||||
// };
|
||||
// int statusCode = await apiServices.updateOwnerDevice(thingID, body);
|
||||
// showSnackBarResponseByStatusCodeNoIcon(
|
||||
// context,
|
||||
// statusCode,
|
||||
// appLocalization(context).notification_update_device_success,
|
||||
// appLocalization(context).notification_update_device_failed,
|
||||
// );
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user