BasicModel class abstract

Basic classics to control data models using maps.

This class already has standard methods to create or update a class based on a Map of data, and also to export a class to a Map. In Dart, JSONs are converted to maps Map<string,dynamic.

See below the methods used publicly to create/update a class, and to generate a Map.

  • fromJson: Create an instance of the class based on a Map.
  • updateValues: Update a class already instantiated with new values contained in a Map.
  • toJson: Export an instantiated class to a Map.

Implementation example:

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() {
    super.writeValues();
    writeValue('id', this.id);
    writeValue('name', this.name);
    writeValue('email', this.email);
    writeValue('phone_number', this.phoneNumber);
  }
}

Constructors

BasicModel()
Standard constructor of class BasicModel.
BasicModel.fromJson(dynamic json)
Generate the class based on a Map (JSON).

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readList<T>(String fieldName, {T? convertion(dynamic value)?, List<T?>? nullValue}) List<T?>?
This method is used to read a list of values from a Map (JSON) and return it in the list of defined type.
readValue<T>(String fieldName, {T? convertion(dynamic value)?, T? nullValue}) → T?
This method is used to read a value from the Map (JSON) data and return it in the defined type.
readValues() → void
This method updates the class data. It is called by the updateValues and BasicModel.fromJson method for creating the class.
toJson({bool exportOnlyChanged = false, bool ignoreNulls = false}) Map<String, dynamic>
This method is used in cases where it is necessary to export the class to a Map (JSON).
toString() String
A string representation of this object.
inherited
updateValues(Map<String, dynamic> map) → void
This method updates the class data with the map passed by parameter.
writeValue(String fieldName, dynamic value, {bool? ignoreNull, bool? exportIfChanged, dynamic convertion(dynamic value)?}) → void
This method is used to save the values in a Map (JSON), this will be used when the class is being exported to a JSON,
writeValues(bool exportOnlyChanged, bool ignoreNulls) → void
This method is used to define the export structure of the class values to a Map.

Operators

operator ==(Object other) bool
The equality operator.
inherited