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