useful_classes

Creator: coderz1093

Last updated:

0 purchases

useful_classes Image
useful_classes Images
Add to Cart

Description:

useful classes

Useful Classes #
Package with useful classes to assist in creating other packages and applications.

Introduction
How to Install
Basic Model
Logger
OnDispose

Introduction #
This package was developed with the intention of maintaining useful classes that are used in other packages, to avoid replicating copies of the same classes.
How to install #
Add the dependency on pubspec.yaml.
Informing ^ at the beginning of the version, you will receive all updates that are made from version 2.0.0 up to the version before 3.0.0.
dependencies:
useful_classes: ^3.0.0
copied to clipboard
Import the package in the source code.
import 'package:useful_classes/useful_classes.dart';
copied to clipboard
Basic Model #
BasicModel is very useful for classes that need to be created from a JSON (on the flutter a JSON is a field and value map) or write a JSON from the instance data.
In the example below, a class with the name UserModel will be created, after that an example of how to create the class from a JSON will be shown and after generating JSON again based on the data contained in the class instance.
Class implementation
class UserModel extends BasicModel {
int id;
String name;
String email;
String phoneNumber;

UserModel() : super();
UserModel.fromJson(json) : super.fromJson(json);

@override
void readValues() {
super.readValues();
this.id = readValue<int>('id');
this.name = readValue<String>('name');
this.email = readValue<String>('email');
this.phoneNumber = readValue<String>('phone_number');
}

@override
void writeValues(bool exportOnlyChanged, bool ignoreNulls) {
writeValue('id', this.id);
writeValue('name', this.name);
writeValue('email', this.email);
writeValue('phone_number', this.phoneNumber);
}
}
copied to clipboard
Import the class based on a JSON
Map<String, dynamic> json = {
'id': 1,
'name': 'Darth Vader',
'email': '[email protected]',
'phone_number': '547889633245'
};

UserModel user = UserModel.fromJson(json);
copied to clipboard
Generate JSON
UserModel user = UserModel()
..name = 'Obi-Wan Kenobi'
..email = '[email protected]'
..phoneNumber = '478998789753'

Map<String, dynamic> json = user.toJson();
copied to clipboard
Logger #
The Logger class is a simple class for printing information on the terminal only when the application is in debug mode.
Below is an example of implementing the logger.
final Logger logger = Logger();

logger.log('simple log');
/// output: simple log

logger.info('information log');
/// output: 💡 information log

logger.warning('warning log');
/// output: ⚠️ warning log

logger.error('error log');
/// output: ⛔ error log
copied to clipboard
If it is necessary to add a prefix when printing all logs, use the prefix parameter in the class constructor.
final Logger logger = Logger(prefix: 'My prefix');
copied to clipboard
OnDispose #
Implement in their classes dispose() method, allowing notify listeners when class is disposed
class Test with OnDispose {

// your class structure

@override
dispose() {
// dispose your class and call `super.dispose()` to notify listeners
super.dispose();
}
}
copied to clipboard
In the example below, the test class will be created and an event added when discarding the class to print the text Object disposed.
final Test test = Test();
test.onDispose.add((object) => print('Object disposed'));

test.dispose();
/// output: Object disposed
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.