flutter_api_model 2.0.3 copy "flutter_api_model: ^2.0.3" to clipboard
flutter_api_model: ^2.0.3 copied to clipboard

Modeling of network requests, which manages input parameters and output parameters, which follows the "In-Out" naming convention.

Features #

Modeling of network requests, which manages input parameters and output parameters, which follows the "In-Out" naming convention.

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 = await UserSearchAPIModel(inUserId: '2024').start();
if (model.hasError) {
  final error = model.outError;
} else {
  final user = model.outUser;
}

Using callback

UserSearchAPIModel(inUserId: '2024').onComplete((model) {
  if (!model.hasError) {
    final user = model.outUser;
  } else {
    final 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.

class SomeAPIModel with APIModel<SomeAPIModel>

2. Using inheritance:

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

class BaseRequestModel extends APIModel<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.

class SomeAPIModel extends BaseRequest with APIModel<SomeAPIModel>

Example: Defining UserSearchAPIModel: #

class UserSearchAPIModel extends BaseRequest<Map> 
      with  APIModel<UserSearchAPIModel>, 
            OutError<FlutterError>,
            LoginNeed {
  UserSearchAPIModel({required this.inUserId});
  /// Input parameter
  String inUserId;
  /// Output result
  User? outUser;

  @override
  load() async {
    try {
      final response = await dio.request('/user/profile');
      outUser = User.converFrom(jsonObject);
    } catch on flutterError (e) {
      outError = e;
    } finally {
      finalize();
    }
  }
}

/// Defines a base type if initialization work is needed
/// Defining `BaseRequest` Class
class BaseRequest<DataType> {
  Dio dio = Dio();

  DataType? data;

  int? code;

  String? msg;

  BaseRequest() {
    dio.options.baseUrl = 'https://base_url.com';
    dio.options.headers = {'token': 'some_token'};
  }

  void fillData(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
mixin LoginNeed {
  bool hasPermission() {
    return isLogin();
  }
}

Using Versions Prior to Dart 3.0 #

3
likes
0
points
50
downloads

Publisher

unverified uploader

Weekly Downloads

Modeling of network requests, which manages input parameters and output parameters, which follows the "In-Out" naming convention.

Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

flutter, meta

More

Packages that depend on flutter_api_model