castValues method

void castValues(
  1. Map<String, Cast>? schema
)

Typecast the values in this archive.

Prefer to override Coding.castMap instead of using this method directly.

This method will recursively type values in this archive to the desired type for a given key. Use this method (or Coding.castMap) for decoding List and Map types, where the values are not Coding objects.

You must import 'package:codable/cast.dart' as cast;.

Usage:

    final dynamicObject = {
      "key": <dynamic>["foo", "bar"]
    };
    final archive = KeyedArchive.unarchive(dynamicObject);
    archive.castValues({
      "key": cast.List(cast.String)
    });

    // This now becomes a valid assignment
    List<String> key = archive.decode("key");

Implementation

void castValues(Map<String, cast.Cast>? schema) {
  if (schema == null) {
    return;
  }

  final caster = cast.Keyed(schema);
  _map = caster.cast(_map);

  if (_objectReference != null) {
    // todo: can optimize this by only running it once
    _objectReference!._map = caster.cast(_objectReference!._map);
  }
}