phase3(foundation): http_util dio5 migration + null-safe models

- http_util.dart: dio4->5 (DioException, nullable params, drop unused statusCode,
  null-safe e.response!.data), countryCode ?? ''
- models/* (39 files): make all instance fields nullable (fromJson initializer-list
  pattern forces nullable); fix internal method usages (cart_info/cart_line_item/
  order/shipping_rate) with !/??
- models/ now passes dart analyze with 0 errors

Widget-level null-safety + remaining utils/store continue in next batch.
This commit is contained in:
2026-07-24 03:45:01 +08:00
parent 1a578c925d
commit 4f9aa4f683
40 changed files with 382 additions and 389 deletions

View File

@@ -14,10 +14,8 @@ import '../constants.dart';
typedef void PostCallback(Response response);
String generateSignature(String string, {String key}) {
if (key == null) {
key = Constants.API_SECRET;
}
String generateSignature(String string, {String? key}) {
key ??= Constants.API_SECRET;
Hmac mac = new Hmac(sha256, utf8.encode(key));
Digest digest = mac.convert(utf8.encode(string + key));
String base64Mac = base64.encode(utf8.encode("$digest"));
@@ -38,16 +36,16 @@ class HttpUtil {
'Http-Device-Type': Utils.getOs(checkWeb: true),
'Http-Api-Branch': 'flutter',
'Http-Language-Code': store.state.locale.languageCode,
'Http-Country-Code': store.state.locale.countryCode,
'Http-Country-Code': store.state.locale.countryCode ?? '',
};
static Future<dynamic> httpGet(String url,
{
Map<String, dynamic> queryParameters,
Map<String, dynamic>? queryParameters,
int businessId = 0,
Function(int, int) receiveProgress,
Function(int, int)? receiveProgress,
bool returnError = false,
Map<String, String> additionalHeaders,
Map<String, String>? additionalHeaders,
}) async {
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
if (additionalHeaders != null) {
@@ -69,7 +67,7 @@ class HttpUtil {
requestUrl = url;
}
Utils.jsonPrettyPrint(queryParameters);
Utils.jsonPrettyPrint(queryParameters ?? {});
Dio dio = Dio();
try {
@@ -84,9 +82,8 @@ class HttpUtil {
// print('response data: ${response.data}');
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
return response.data;
} on DioError catch(e) {
} on DioException catch(e) {
print('HttpGet failed, request: $requestUrl, headers: $localHeaders, error ${e.response}');
if (returnError) {
return e;
@@ -97,14 +94,14 @@ class HttpUtil {
static Future<dynamic> httpPost(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
Map<String, dynamic>? queryParameters,
int businessId = 0,
bool isFormData = false,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
FormData formData,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
Map<String, String>? additionalHeaders,
Map<String, dynamic>? body,
FormData? formData,
Function(int, int)? sendProgress,
Function(int, int)? receiveProgress,
bool returnError = false,
}) async {
@@ -130,15 +127,15 @@ class HttpUtil {
}
// Don't print body will cause upload Multipart file failed
//Utils.jsonPrettyPrint(body);
//Utils.jsonPrettyPrint(body ?? {});
Dio dio = Dio();
dio.interceptors.add(LogInterceptor());
try {
Response response = await dio.post(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
data: isFormData ? ((formData != null)?formData:FormData.fromMap(body)) : json.encode(body),
queryParameters: queryParameters ?? {},
data: isFormData ? ((formData != null)?formData:FormData.fromMap(body ?? {})) : json.encode(body),
options: Options(
headers: localHeaders,
contentType: isFormData ? Headers.formUrlEncodedContentType : Headers.jsonContentType,
@@ -149,17 +146,16 @@ class HttpUtil {
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e, stacktrace) {
} on DioException catch(e, stacktrace) {
if (e.response != null) {
try {
Utils.jsonPrettyPrint(e.response.data);
Utils.jsonPrettyPrint(e.response!.data);
} catch (err) {
print(e.response.data);
print(e.response!.data);
}
}
if (returnError) {
@@ -171,12 +167,12 @@ class HttpUtil {
static Future<dynamic> httpPut(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
Map<String, dynamic>? queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
Map<String, String>? additionalHeaders,
Map<String, dynamic>? body,
Function(int, int)? sendProgress,
Function(int, int)? receiveProgress,
bool returnError = false,
}) async {
@@ -207,7 +203,7 @@ class HttpUtil {
try {
Response response = await dio.put(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
queryParameters: queryParameters ?? {},
data: json.encode(body),
options: Options(
headers: localHeaders,
@@ -218,14 +214,13 @@ class HttpUtil {
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
} on DioException catch(e) {
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
Utils.jsonPrettyPrint(e.response!.data);
}
if (returnError) {
return e;
@@ -236,12 +231,12 @@ class HttpUtil {
static Future<dynamic> httpPatch(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
Map<String, dynamic>? queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Function(int, int) sendProgress,
Function(int, int) receiveProgress,
Map<String, String>? additionalHeaders,
Map<String, dynamic>? body,
Function(int, int)? sendProgress,
Function(int, int)? receiveProgress,
bool returnError = false,
}) async {
@@ -268,13 +263,13 @@ class HttpUtil {
requestUrl = url;
}
Utils.jsonPrettyPrint(body);
Utils.jsonPrettyPrint(body ?? {});
Dio dio = Dio();
try {
Response response = await dio.patch(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
queryParameters: queryParameters ?? {},
data: json.encode(body),
options: Options(
headers: localHeaders,
@@ -285,14 +280,13 @@ class HttpUtil {
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
} on DioException catch(e) {
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
Utils.jsonPrettyPrint(e.response!.data);
}
if (returnError) {
return e;
@@ -303,10 +297,10 @@ class HttpUtil {
static Future<dynamic> httpDelete(String url, PostCallback callback,
{
Map<String, dynamic> queryParameters,
Map<String, dynamic>? queryParameters,
int businessId = 0,
Map<String, String> additionalHeaders,
Map<String, dynamic> body,
Map<String, String>? additionalHeaders,
Map<String, dynamic>? body,
bool returnError = false,
}) async {
@@ -333,13 +327,13 @@ class HttpUtil {
requestUrl = url;
}
Utils.jsonPrettyPrint(body);
Utils.jsonPrettyPrint(body ?? {});
Dio dio = Dio();
try {
Response response = await dio.delete(
requestUrl,
queryParameters: queryParameters == null ? {} : queryParameters,
queryParameters: queryParameters ?? {},
data: json.encode(body),
options: Options(
headers: localHeaders,
@@ -348,14 +342,13 @@ class HttpUtil {
// Utils.jsonPrettyPrint(response.data);
int statusCode = response.statusCode;
if (callback != null) {
callback(response);
}
return response.data;
} on DioError catch(e) {
} on DioException catch(e) {
if (e.response != null) {
Utils.jsonPrettyPrint(e.response.data);
Utils.jsonPrettyPrint(e.response!.data);
}
if(returnError) {
return e;