Skip to content

Repository files navigation

Features

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.

Getting started

Installing

Add the following dependency to your pubspec.yaml file:

dependencies:
flutter_api_model: latest_version

Importing

Import the package into your Dart code:

import'package:flutter_api_model/flutter_api_model.dart';

Supports both await and callback formats for flexibility.

Using await

final model =awaitUserSearchAPIModel(inUserId:'2024').start();
if (model.hasError) {
error = model.outError;
} else {
user = model.outUser;
}

Using callback

UserSearchAPIModel(inUserId:'2024').onComplete((model) {
if (!model.hasError) {
user = model.outUser;
} else {
error = model.outError;
}
}).start();

Class definition

Naming rules:

  • The prefix of the input parameter: in
    • (inUsername, inPassword)
  • The prefix of the return value: out
    • (outLoginUser)

There are three ways to define APIModel:

1. Using mixin:

Ideal for scenarios without special initialization requirements.

classSomeAPIModelwithAPIModel<SomeAPIModel>

2. Using inheritance:

Suitable for defining a base network request class that extends APIModel.

classBaseRequestModelextendsAPIModel<SomeAPIModel>

3. Using a combination of mixin and inheritance (recommended):

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>

Example: Defining UserSearchAPIModel:

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();
}
}

Using Versions Prior to Dart 3.0

About

High-level modeling of network requests, which manages input parameters and output parameters, following the "In-Out" naming convention.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages