deltaBoolAttributeCodec function

TextAttributeDeltaCodec deltaBoolAttributeCodec(
  1. String key,
  2. TextAttribute instance
)

Convenience function to create a codec for a TextAttribute with a boolean value.

Useful for toggleable attributes like bold or italic.

Implementation

TextAttributeDeltaCodec deltaBoolAttributeCodec(
  String key,
  TextAttribute instance,
) {
  return TextAttributeDeltaCodec(
    key: key,
    decoder: ClosureConverter<dynamic, TextAttribute>((dynamic v) {
      if (v is! bool) {
        throw FormatException(
            'Expected attribute of type bool, but was ${v.runtimeType}');
      }
      if (!v) {
        throw const FormatException(
            'Boolean attributes should only be serialized when their value is true.');
      }
      return instance;
    }),
    appliesTo: (t) => t.runtimeType == instance.runtimeType,
    encoder: ClosureConverter((_) => true),
  );
}