Skip to content

Instances

Efra Espada edited this page Jan 30, 2024 · 2 revisions

Sometimes working with data sets can be complicated when we try to convert them into instances of classes. As you will see below, creating instances is very simple.

Create new instances

SimpleSample? sample = {
  'id': 'id',
  'numberContent': 1,
  'integerContent': 2,
  'doubleContent': 3.0,
  'stringContent': 'string',
  'booleanContent': true,
}.asNullableInstance();

final other = {
  'id': 'other',
  'numberContent': 1,
  'integerContent': 2,
  'doubleContent': 3.0,
  'stringContent': 'string',
  'booleanContent': true,
}.asNullableInstance<SimpleSample>();

Not-null instances

SimpleSample sample = {
  'id': 'id',
  'numberContent': 1,
  'integerContent': 2,
  'doubleContent': 3.0,
  'stringContent': 'string',
  'booleanContent': true,
}.asInstance(SimpleSample());

Working with non-null instances is complicated because there may be some kind of error in the instance creation process and the result is null, so the only option is to offer a default instance.

Create a list of instances

List<SimpleSample> samples = [
  {
    'id': 'id',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  },
  {
    'id': 'other',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  }
].asInstanceList();

final otherList = [
  {
    'id': 'id',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  },
  {
    'id': 'other',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  }
].asInstanceList<SimpleSample>();

Create a map of instances

Map<String, SimpleSample> sampleMap = {
  'id': {
    'id': 'id',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  },
  'other': {
    'id': 'other',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  }
}.asInstanceMap();

final otherMap = {
  'id': {
    'id': 'id',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  },
  'other': {
    'id': 'other',
    'numberContent': 1,
    'integerContent': 2,
    'doubleContent': 3.0,
    'stringContent': 'string',
    'booleanContent': true,
  }
}.asInstanceMap<SimpleSample>();

Other types of instances

You can create any other type of object. Basic instances, lists, or maps.

final boolList = [
  false,
  false,
  true,
].asBoolList();
final doubleMap = [
  0.0,
  1.0,
  3.0,
].asDoubleList();

Conversion safety

final doubleValue = 'string'.asDouble();
print(doubleValue); // prints 0.0
final boolValue = 77.asBool(defaultValue: true);
print(boolValue); // prints true

For example, with this class:

import 'generated/model.g.dart';

class Sample extends SampleGen {
  @override
  @Field(
    name: 'id',
    primary: true,
  )
  String id = '';

  @override
  @Field(name: 'numberContent')
  num numberContent = 0;  

  @override
  @Field(name: 'other')
  OtherSample other = 0;  

  Sample();
}

class OtherSample extends OtherSampleGen {
  @override
  @Field(
    name: 'id',
    primary: true,
  )
  String id = '';

  @override
  @Field(name: 'boolContent')
  bool boolContent = 0;  

  OtherSample();
}
SimpleSample? sample = {
  'id': 'id',
  'numberContent': 'blabla',
  'other': {
    'id': 'id',
    'boolContent': 'blabla',
  }
}.asNullableInstance();

print(sample.numberContent); // prints 0
print(other.boolContent); // prints false