readRuntimeObject<T extends Core<CoreContext>> function

T readRuntimeObject<T extends Core<CoreContext>>(
  1. BinaryReader reader,
  2. [T instance]


T readRuntimeObject<T extends Core<CoreContext>>(BinaryReader reader,
    [T instance]) {
  int coreObjectKey = reader.readVarUint();

  var object = instance ?? RiveCoreContext.makeCoreInstance(coreObjectKey);
  if (object is! T) {
    return null;

  while (true) {
    int propertyKey = reader.readVarUint();
    if (propertyKey == 0) {
      // Terminator.
    int propertyLength = reader.readVarUint();
    Uint8List valueBytes =;

    var fieldType = RiveCoreContext.coreType(propertyKey);
    if (fieldType == null) {
      // This is considered an acceptable failure. A runtime may not support
      // the same properties that were exported. The older object could still
      // function without them, however, so it's up to the implementation to
      // make sure that major versions are revved when breaking properties are
      // added. Note that we intentionally first read the entire value bytes
      // for the property so we can advance as expected even if we are
      // skipping this value.

    // We know what to expect, let's try to read the value. We instance a new
    // reader here so that we don't overflow the exact length we're allowed to
    // read.
    var valueReader = BinaryReader.fromList(valueBytes);

    // This will attempt to set the object property, but failure here is
    // acceptable.
        object, propertyKey, fieldType.deserialize(valueReader));
  return object as T;