truesight_core 2401.24.3 truesight_core: ^2401.24.3 copied to clipboard
Core functions for TrueSight packages
Features #
- JSON Handling
- Data filter types compatible with the backend
- Converting HTTP responses to various formats
Installation #
To install the package:
flutter pub add truesight_core
How to Use #
JSON Handling #
The package defines the following JSON data types:
JsonBoolean
: forbool
typeJsonDate
: forDateTime
typeJsonList
: forList<Object>
typeJsonNumber
: fornum
typeJsonObject
: forObject
typeJsonString
: forString
type
Model Definition
Example:
class User extends DataModel {
//
JsonBoolean isAdmin = JsonBoolean("isAdmin");
JsonString username = JsonString("username");
JsonDate dateOfBirth = JsonDate("dateOfBirth");
//
}
Usage:
JsonType fieldName = JsonType("jsonFieldName");
To convert to and from JSON:
User.fromJSON(Map<String, dynamic> json);
final Map<String, dynamic> json = User.toJSON(); // Dart Map
final String jsonString = User.toString(); // JSON string
AdvancedFilter #
Filter types:
DateFilter
: for date-type fieldsStringFilter
: forstring
type fieldsNumberFilter
: for numeric fieldsGuidFilter
: for key fields (primary | foreign) of Guid typeIdFilter
: for key fields (primary | foreign) of integer Id type
Example filter class:
class UserFilter extends DataFilter {
StringFilter username = StringFilter();
DateFilter dateOfBirth = DateFilter();
// Default fields:
int skip;
int take;
String? orderBy;
OrderType? orderType;
}
OrderType
There are 2 order types:
OrderType.asc; // Ascending
OrderType.desc; // Descending
HTTPRepository #
A repository is a class containing methods to call corresponding APIs for a Controller / API group at the backend.
Example of creating a repository:
@singleton // This is a GetIt annotation => Make the class singleton instance
class AccountRepository extends HttpRepository {
// This tells the repository to use interceptor to transform every request
@override
bool get useInterceptor => true;
// This is InterceptorsWrapper, used along with `useInterceptor = true`
@override
InterceptorsWrapper interceptorsWrapper = globalInterceptorsWrapper;
// Base URL, for example: [https://example.com](https://example.com)
// Should use `flutter_dotenv` to configure the apiBaseUrl,
@override
String get baseUrl => dotenv.apiBaseUrl;
AccountRepository() : super("api/prefix");
Future<AppUser> login(String username, String password) {
return post(
url("login"),
data: {
"username": username,
"password": password,
},
).then(
(Response response) => response.body<AppUser>(AppUser),
);
}
Future<AppUser> getProfile() {
return post(
url("get"),
data: {},
).then(
(Response response) => response.body<AppUser>(AppUser),
);
}
Future<Map<String, dynamic>> refreshToken() {
return post(
url("refresh-token"),
data: {},
).then(
(Response response) => response.data,
);
}
}
This document outlines the core features of the Truesight package for Dart and Flutter, detailing its JSON handling capabilities, filter types, and methods for handling HTTP responses, along with installation and usage instructions.