value property

Object? get value

Gets the value of the late variable, initializing it if necessary

Implementation

Object? get value {
  if (!_isInitialized) {
    if (_initializer != null) {
      // Lazy initialization. A read while the initializer runs means it
      // depends on itself (`static const a = b; static const b = a;`):
      // report it instead of recursing until the stack overflows. An
      // initializer that throws leaves the variable uninitialized, so the
      // next read runs it again, as in Dart.
      if (_isInitializing) {
        throw LateInitializationError(
          "LateInitializationError: '$_name' is read during its initialization "
          '(its initializer depends on itself).',
        );
      }
      _isInitializing = true;
      try {
        _value = _widened(_initializer());
      } finally {
        _isInitializing = false;
      }
      _isInitialized = true;
      if (Logger.isDebug) {
        Logger.debug(
          "[LateVariable] Lazy initialized '$_name' with value: $_value",
        );
      }
    } else {
      // Late variable without initializer accessed before assignment
      throw LateInitializationError(
        "LateInitializationError: Late variable '$_name' without initializer is accessed before being assigned.",
      );
    }
  }
  return _value;
}
set value (Object? newValue)

Set the value

Implementation

set value(Object? newValue) {
  if (_isInitialized && _isFinal) {
    throw LateInitializationError(
      "LateInitializationError: Field '$_name' has already been initialized.",
    );
  }
  _value = _widened(newValue);
  _isInitialized = true;
}