ImmutableKind<T> class sealed

Kind for immutable objects.

There are four possible constructors:

Optimization tricks

Example

import 'package:kind/kind.dart';

void main() {
  //
  // Encode/decode JSON trees:
  //
  final company = Company.kind.decodeJsonTree({
    'name': 'Flutter App Development Experts',
    'shareholders': [
      {
        '@type': 'Person',
        'firstName': 'Alice',
        'lastName': 'Smith',
      },
      {
        '@type': 'Person',
        'firstName': 'Bob',
      },
    ],
  });
  print("${company.name} has ${company.shareholders.length} shareholders.");

  //
  // We have `==` and `hashCode` because we extended `HasKind`:
  //
  print(company.shareholders[1] == Person(firstName: 'Bob')); // --> true

  //
  // We have `toString()` because we extended `HasKind`:
  //
  print(company.toString());
  // Prints:
  //   Company(
  //     "Flutter App Development Experts",
  //     shareholders: [
  //       Person(
  //         firstName: 'John',
  //         lastName: 'Doe',
  //       ),
  //       Person(firstName: 'Bob'),
  //     ],
  //   )
}

//
// Example class #1:
//   "A static `_walk` function"
//
class Company extends Shareholder {
  static const kind = ImmutableKind<Company>(
    name: 'Company',
    blank: Company(''),
    walk: _walk,
  );

  @override
  final String name;

  final List<Shareholder> shareholders;

  const Company(this.name, {this.shareholders = const []});

  @override
  Kind<Company> get runtimeKind => kind;

  static Company _walk(Mapper f, Company t) {
    final name = f.positional(t.name, 'name');
    final shareholders = f(
      t.shareholders,
      'shareholders',
      kind: const ListKind(
        elementKind: Shareholder.kind,
      ),
    );

    // The following is a performance optimization we recommend:
    if (f.canReturnSame) {
      return t;
    }

    // Finally, construct a new instance:
    return Company(
      name,
      shareholders: shareholders,
    );
  }
}

//
// Example #2:
//   "A class that extends `Walkable`"
//
class Person extends Shareholder with Walkable {
  static const kind = ImmutableKind<Person>.walkable(
    name: 'Person',
    blank: Person(firstName: ''),
  );

  /// First name
  final String firstName;

  /// First name
  final String lastName;

  const Person({
    required this.firstName,
    this.lastName = '',
  });

  @override
  String get name => '$firstName $lastName';

  @override
  Kind<Person> get runtimeKind => kind;

  @override
  Person walk(Mapper f) {
    final firstName = f.required(
      this.firstName,
      'firstName',
      kind: const StringKind.singleLineShort(),
    );
    final lastName = f(
      this.lastName,
      'lastName',
      kind: const StringKind.singleLineShort(),
    );
    if (f.canReturnSame) {
      return this;
    }
    return Person(
      firstName: firstName,
      lastName: lastName,
    );
  }
}

//
// Example #3:
//   "A polymorphic class"
//
abstract class Shareholder extends HasKind {
  static const kind = PolymorphicKind<Shareholder>.sealed(
    defaultKinds: [
      Person.kind,
      Company.kind,
    ],
  );

  const Shareholder();

  String get name;
}
Inheritance

Constructors

ImmutableKind({String? name, String? jsonName, required T blank, required WalkFunction<T> walk, Function? fromJson, Object? toJson(T object)?, Map<String, T>? constantsByName})
Constructs a kind for a class that has a constant constructor.
const
factory
ImmutableKind.jsonSerializable({String? name, String? jsonName, Map<String, Kind>? kinds, T walk(Mapper f, T t)?, required T fromJson(Map<String, dynamic> json), Map<String, dynamic> toJson(T object)?, Map<String, T>? constantsByName})
Creates a kind for any JSON-serializable class.
factory
ImmutableKind.walkable({String? name, String? jsonName, required T blank, Function? fromJson, Object? toJson(T object)?, Map<String, T> constantsByName})
Constructs a kind for a class that implements Walkable.
const
factory
ImmutableKind.withConstructor({String? name, String? jsonName, required T constructor(), required WalkFunction<T> walk, Function? fromJson, Object? toJson(T object)?, Map<String, T> constantsByName})
Constructs a kind for a class that has a non-constant constructor.
const
factory

Properties

constantsByName Map<String, T>
Constant instances.
final
dartType Type
Type T.
no setterinherited
defaultValueMirror InstanceMirror
InstanceMirror for the default value.
no setterinherited
equality → Equality
Equality for the kind;
no setterinherited
examples Iterable<T>
Examples of instances that are valid (isValidDynamic).
no setterinherited
examplesThatAreInvalid Iterable<T>
Examples of instances that are NOT valid (isValidDynamic).
no setterinherited
examplesWithoutValidation Iterable<T>
Interesting examples of instances (may be valid or invalid).
no setteroverride
hashCode int
The hash code for this object.
no setteroverride
isNullable bool
Whether this kind is nullable.
no setterinherited
isPrimitive bool
Whether instances of the kind can't have references to other instances.
no setterinherited
isSealed bool
Whether subclasses are possible;
final
jsonName String?
JSON identifier of the class.
finalinherited
name String
Dart identifier of the class.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
traits List<Trait>
Traits of the kind.
finalinherited

Methods

asType(Object? value) → T
Casts value to T.
inherited
checkDeclaration() → void
Checks that the declaration makes sense.
inherited
checkInstance(Object? value) → void
inherited
checkValid(T instance) → void
override
checkValidDynamic(Object? instance) → void
Throws ArgumentError error if instance is not valid (isValidDynamic).
inherited
clone(T instance) → T
Clones the instance.
override
compare(T left, T right) int
Compares two values.
override
debugString(T instance) String
Constructs a string for debugging instance.
override
decodeJsonTree(Object? json) → T
Converts json (any JSON tree) to an instance of T.
override
decodeString(String string) → T
Decodes string to an instance of T.
inherited
encodeJsonTree(T instance) Object?
Converts instance to a JSON tree.
override
encodeString(T instance) String
Converts instance to a string.
inherited
isDefaultValue(Object? instance) bool
Determines whether the argument is a default value of this kind.
inherited
isInstance(Object? instance) bool
Determines whether the argument is an instance of T.
inherited
isInstanceOfList(Object? instance) bool
Determines whether the argument is an instance of List<T>.
inherited
isInstanceOfSet(Object? instance) bool
Determines whether the argument is an instance of Set<T>.
inherited
isNullableSubKind(Kind other, {bool andNotEqual = true}) bool
Tells whether the argument is instance of Kind<T> and dartType values are different.
inherited
isSubKind(Kind other, {bool andNotEqual = true}) bool
Tells whether the argument is instance of Kind<T> and dartType values are different.
inherited
isValid(T instance) bool
Tells whether the instance is valid.
override
isValidDynamic(Object? instance) bool
Tells whether the instance is valid.
inherited
map(Mapper mapper, T object) → T
Maps object to a new instance of T.
mapDefault(Mapper mapper) → T
Maps the default value with the mapper to a new instance of T.
memorySize(T instance) int
Estimates memory usage of instance.
inherited
memorySizeWith(MemoryCounter counter, T instance) → void
Estimates memory usage with an instance of MemoryCounter.
override
newInstance() → T
Constructs a new instance of the default value.
inherited
newList(int length, {bool growable = true}) List<T>
Constructs a new list.
inherited
newListFrom(Iterable<T> iterable, {bool growable = true}) List<T>
Constructs a new list from iterable.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
permute(T instance) → T
Generates another instance with some deterministic function.
override
register() → void
Registers this kind so that it will be visible in Kind.all.
inherited
toList() Kind<List<T>>
Constructs Kind for List<T>.
inherited
toNonNullable() Kind<T>
Returns a non-nullable kind.
inherited
toNullable() Kind<T?>
Constructs Kind for T?.
inherited
toPolymorphic() PolymorphicKind<T>
Constructs a PolymorphicKind for this kind.
inherited
toSet() Kind<Set<T>>
Constructs Kind for Set<T>.
inherited
toString() String
A string representation of this object.
inherited

Operators

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