High-level modeling of network requests, which manages input parameters and output parameters, following the "In-Out" naming convention.APIModel doesn't handle network requests directly; instead, it excels at network layer abstraction.
Add the following dependency to your pubspec.yaml file:
dependencies:
flutter_api_model: latest_versionImport the package into your Dart code:
import'package:flutter_api_model/flutter_api_model.dart';final model =awaitUserSearchAPIModel(inUserId:'2024').start();
if (model.hasError) {
error = model.outError;
} else {
user = model.outUser;
}UserSearchAPIModel(inUserId:'2024').onComplete((model) {
if (!model.hasError) {
user = model.outUser;
} else {
error = model.outError;
}
}).start();
- The prefix of the input parameter: in
- (inUsername, inPassword)
- The prefix of the return value: out
- (outLoginUser)
Ideal for scenarios without special initialization requirements.
classSomeAPIModelwithAPIModel<SomeAPIModel>Suitable for defining a base network request class that extends APIModel.
classBaseRequestModelextendsAPIModel<SomeAPIModel>Best for custom network requests, data processing, and separation of concerns.
For example, BaseRequest can handle Dio operations and data transformation,
while APIModel provides request flow and encapsulation.
classSomeAPIModelextendsBaseRequestwithAPIModel<SomeAPIModel>classUserSearchAPIModelextendsBaseRequest<Map> withAPIModel<UserSearchAPIModel>, OutError<UserSearchAPIModel, FlutterError>,
LoginNeed,
CancelEnable {
UserSearchAPIModel({requiredthis.inUserId});
/// Input parameterString inUserId;
/// Output resultUser? outUser;
@overrideload() async {
try {
final response =await dio.request('/user/profile', cancelToken: cancelToken);
outUser =User.converFrom(jsonObject);
} onFlutterErrorcatch (e) {
outError = e;
} catch (e) {
if (CancelToken.isCancel(e) ==false) { // Handler errorthrow e;
}
} finally {
finalize();
}
}
}
/// Defines a base type if initialization work is needed/// Defining `BaseRequest` ClassclassBaseRequest<DataType> {
final dio =Dio();
final cancelToken =CancelToken(); DataType? data;
int? code;
String? msg;
BaseRequest() {
dio.options.baseUrl ='https://base_url.com';
dio.options.headers = {'token':'some_token'};
}
voidfillData(Dio.Response response) {
data =getData(response);
code = response.statusCode;
msg =getMsg(response);
}
}
/// Defines a mixin to override the `hasPermission` method, blocking calls when the user is not logged in./// Defining `LoginNeed` Mixin.mixinLoginNeed<T> onAPIModel<T> {
@overrideboolhasPermission() {
returnisLogin();
}
didBlock() {
print('API request was blocked.');
}
}
/// Implementing Cancelable Requests with `CancelEnable` Mixin@optionalTypeArgsmixinCancelEnable<T> onAPIModel<T>, BaseRequest {
@overrideboolisCancellable() {
returntrue;
}
@overridevoidcancel() {
super.cancel();
cancelToken.cancel();
}
}
- Go to the repository to download the source code
- Manually import the
libfolder and rename it toflutter_api_model - Locate the file
api_model.dart, and changeabstract mixin class APIModel<T>toabstract class APIModel<T>