renderStateData function

void renderStateData(
  1. LiveBlock block,
  2. Map<String, Object?> payload, {
  3. List<LiveStateField>? fields,
  4. String? unavailable,
  5. bool full = false,
  6. String target = 'metro live:run data',
})

Adds the data view of a state.data payload to block: what the page is, the data it was opened with, and the fields its state declares.

unavailable explains why fields is null when the app couldn't be read. Values are shortened to one line unless full. target is how to run the command again for another state.

Implementation

void renderStateData(
  LiveBlock block,
  Map<String, Object?> payload, {
  List<LiveStateField>? fields,
  String? unavailable,
  bool full = false,
  String target = 'metro live:run data',
}) {
  final Map<String, Object?> state = _map(payload['state']);
  block.heading('${state['widget'] ?? 'The page'} ยท ${state['kind'] ?? '-'}');

  final Object? actions = state['actions'];
  final List<List<String>> rows = [
    if (state['name'] != null) ['state', '${state['name']}'],
    if (payload['route'] != null) ['route', '${payload['route']}'],
    if (state['controller'] != null) ['controller', '${state['controller']}'],
    if (actions is List && actions.isNotEmpty) ['actions', actions.join(', ')],
  ];
  final int width = rows
      .map((List<String> row) => row.first.length)
      .fold(0, (int a, int b) => a > b ? a : b);
  for (final List<String> row in rows) {
    block.plain('  ${row.first.padRight(width + 2)}${row.last}');
  }

  block
    ..plain('')
    ..heading('data');
  if (state['data'] != null) {
    block.json(state['data']);
  } else if (state['controller'] == null) {
    // data() reads the route arguments off a NyStatefulWidget's controller.
    block.note(
      'Nothing: only a NyStatefulWidget is given the data a route carries',
    );
  } else {
    block.note('No data was passed to this page');
  }

  if (state['queryParameters'] != null) {
    block
      ..plain('')
      ..heading('queryParameters')
      ..json(state['queryParameters']);
  }

  if (unavailable != null) {
    block
      ..plain('')
      ..heading('fields')
      ..note(unavailable);
  } else if (fields != null) {
    block.plain('');
    renderStateFields(block, fields, full: full);
  }

  renderOtherStates(block, payload, target: target);
}