fix : dio call

This commit is contained in:
2025-05-17 09:08:46 +03:30
parent 3a017b5956
commit b5d6055aa7
6 changed files with 159 additions and 18 deletions

View File

@@ -34,13 +34,15 @@ class AuthRepositoryImpl implements AuthRepository {
@override
Future<CaptchaResponseModel?> captcha() async {
final response = await safeCall(
call:
() async => await _httpClient.post<CaptchaResponseModel>(
'captcha/',
headers: {'Content-Type': 'application/json'},
fromJson: CaptchaResponseModel.fromJson
),
final response = await safeCall<CaptchaResponseModel?>(
call: () async {
var res = await _httpClient.post<CaptchaResponseModel?>(
'captcha/',
fromJson: CaptchaResponseModel.fromJson,
);
return res.data;
},
onSuccess: (response) {
iLog(response);
},
@@ -48,8 +50,7 @@ class AuthRepositoryImpl implements AuthRepository {
throw Exception('Error during captcha : $error');
},
);
return response?.data;
return response;
}
@override

View File

@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import 'package:rasadyar_auth/data/di/auth_di.dart';
import 'package:rasadyar_auth/data/models/response/captcha/captcha_response_model.dart';
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
import 'package:rasadyar_core/core.dart';
class CaptchaWidgetLogic extends GetxController
with StateMixin<CaptchaResponseModel> {
TextEditingController textController = TextEditingController();
GlobalKey<FormState> formKey = GlobalKey<FormState>();
AuthRepositoryImpl authRepository = diAuth.get<AuthRepositoryImpl>();
@override
void onInit() {
super.onInit();
getCaptcha();
}
@override
void onClose() {
super.onClose();
}
Future<void> getCaptcha() async {
change(null, status: RxStatus.loading());
safeCall(
call: () async => await authRepository.captcha(),
onSuccess: (value) {
change(value, status: RxStatus.success());
},
onError: (error, stackTrace) {
change(null, status: RxStatus.error(error.toString()));
},
);
}
}

View File

@@ -0,0 +1,98 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_auth/presentation/widget/clear_button.dart';
import 'package:rasadyar_core/core.dart';
import 'logic.dart';
class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
CaptchaWidget({super.key});
final CaptchaWidgetLogic logic = Get.put(CaptchaWidgetLogic());
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 135,
height: 48,
clipBehavior: Clip.antiAliasWithSaveLayer,
decoration: BoxDecoration(
color: AppColor.whiteNormalHover,
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(8),
),
child: Expanded(child:controller.obx((state) {
return Container(color: Colors.blue,);
},
onLoading: const Center(
child: CircularProgressIndicator(
color: AppColor.blueNormal,
strokeWidth: 2,
),
),
)),
),
const SizedBox(height: 20),
IconButton(
padding: EdgeInsets.zero,
onPressed: controller.getCaptcha,
icon: Icon(CupertinoIcons.refresh, size: 16),
),
Expanded(
child: Form(
key: controller.formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: TextFormField(
controller: controller.textController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
gapPadding: 11,
),
labelText: 'کد امنیتی',
labelStyle: AppFonts.yekan13,
errorStyle: AppFonts.yekan10.copyWith(
color: AppColor.redNormal,
fontSize: 8,
),
suffixIconConstraints: BoxConstraints(
maxHeight: 24,
minHeight: 24,
maxWidth: 24,
minWidth: 24,
),
suffix:
controller.textController.text
.trim()
.isNotEmpty
? clearButton(() => controller.textController.clear())
: null,
counterText: '',
),
keyboardType: TextInputType.numberWithOptions(
decimal: false,
signed: false,
),
maxLines: 1,
maxLength: 6,
onChanged: (value) {},
validator: (value) {
if (value == null || value.isEmpty) {
return 'کد امنیتی را وارد کنید';
}
/*if (value != controller.captchaCode.toString()) {
return '⚠️کد امنیتی وارد شده اشتباه است';
}*/
return null;
},
style: AppFonts.yekan13,
),
),
),
],
);
}
}

View File

@@ -1,10 +1,9 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_core/infrastructure/remote/interfaces/i_form_data.dart';
import 'dio_form_data.dart';
import 'dio_response.dart';
import 'interfaces/i_http_client.dart';
class DioRemote implements IHttpClient {
@@ -29,7 +28,7 @@ class DioRemote implements IHttpClient {
Map<String, String>? headers,
ProgressCallback? onReceiveProgress,
}) async {
final response = await _dio.get<T>(
final response = await _dio.get(
path,
queryParameters: queryParameters,
options: Options(headers: headers),
@@ -48,7 +47,7 @@ class DioRemote implements IHttpClient {
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
final response = await _dio.post<T>(
final response = await _dio.post(
path,
data: data,
queryParameters: queryParameters,
@@ -57,6 +56,8 @@ class DioRemote implements IHttpClient {
onReceiveProgress: onReceiveProgress,
);
eLog(response.toString());
if (fromJson != null) {
final rawData = response.data;
final parsedData =
@@ -65,7 +66,7 @@ class DioRemote implements IHttpClient {
return DioResponse<T>(response);
}
return DioResponse<T>(response);
return DioResponse<T>(response );
}
@override
@@ -77,7 +78,7 @@ class DioRemote implements IHttpClient {
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
final response = await _dio.put<T>(
final response = await _dio.put(
path,
data: data,
queryParameters: queryParameters,
@@ -124,7 +125,7 @@ class DioRemote implements IHttpClient {
Map<String, String>? headers,
ProgressCallback? onSendProgress,
}) async {
final response = await _dio.post<T>(
final response = await _dio.post(
path,
data: (formData as DioFormData).raw,
options: Options(headers: headers, contentType: 'multipart/form-data'),

View File

@@ -2,7 +2,7 @@ import 'interfaces/i_http_response.dart';
import 'package:dio/dio.dart';
class DioResponse<T> implements IHttpResponse<T> {
final Response<T> _response;
final Response<dynamic> _response;
DioResponse(this._response);

View File

@@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:rasadyar_core/core.dart';
typedef AsyncCallback<T> = Future<T> Function();
typedef ErrorCallback = void Function(dynamic error, StackTrace? stackTrace);
@@ -27,12 +28,14 @@ Future<T?> safeCall<T>({
final result = await call();
iLog(result.toString());
if (showSuccess) {
(onShowSuccessMessage ?? _defaultShowSuccessMessage)();
}
onSuccess?.call(result);
return result; // اضافه کردن بازگشت نتیجه
return result;
} catch (error, stackTrace) {
if (showError) {