load method

Future<void> load()

Seeds the form. Create has nothing to fetch; edit reads the record, whose attributes carry the form's fields since Task 1 of P2.

Implementation

Future<void> load() async {
  // A debounce scheduled against the form being replaced is already
  // pointless — it would post the outgoing values and swap in components
  // for a form nobody is looking at any more.
  _stateTimer?.cancel();

  final loadId = ++_loadId;
  // A `/state` response still in flight describes the pre-reload form.
  ++_stateId;

  _status = LoadStatus.loading;
  _errorMessage = null;
  _isUnauthenticated = false;
  // Errors from the submission of a form that is being re-seeded would
  // otherwise sit over values they no longer describe.
  _formError = null;
  _fieldErrors = const {};
  _components = resource.form;
  notifyListeners();

  try {
    final record = recordId == null
        ? null
        : await source.record(resource.key, recordId!);

    if (loadId != _loadId) return;

    _record = record;
    _values = FormValues.initial(
      _components,
      from: record?.attributes ?? const {},
    );
    _status = LoadStatus.success;
  } catch (e) {
    if (loadId != _loadId) return;

    if (e is FilamentTransportException && e.statusCode == 401) {
      _isUnauthenticated = true;
    }
    _errorMessage = messageOf(e);
    _status = LoadStatus.failure;
  }

  _notify();
}