flutter_api_model 2.0.1 copy "flutter_api_model: ^2.0.1" to clipboard
flutter_api_model: ^2.0.1 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.

Naming rules: #

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

Getting started #

Installing

dependencies:
  flutter_api_model: latest_version

Importing

import 'package:flutter_api_model/flutter_api_model.dart';

Using await #

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

Using callback #

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

Class definition #

class ProfileAPIModel extends BaseAPI<Map> with ModelAPI<ProfileAPIModel>, APIWithLoginNeed {
  ProfileAPIModel({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 (e) {
      outError = e;
    } finally {
      finalize();
    }
  }
}

/// Defines a base type if initialization work is needed
class BaseAPI<T> {
  Dio dio = Dio();

  T? jsonObject;

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

  void fillJson(String jsonString) {
    jsonObject = convert(jsonString);
  }
}

mixin APIWithLoginNeed {
  bool hasPermission() {
    return isLogin();
  }
}

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

More

Packages that depend on flutter_api_model