updateState<T> function

void updateState<T>(
  1. dynamic name, {
  2. dynamic data,
  3. dynamic setValue(
    1. T? currentValue
    )?,
})

Update's the state of a NyState Widget in your application. Provide the name of the state and then return a value in the callback setValue.

Example using data param updateState

Example in your NyState widget @override stateUpdated(dynamic data) async { print(data'value'); // 10 }

Example using setValue param updateState

updateState

Example in your NyState widget @override stateUpdated(dynamic data) async { print(data); // 2 }

Implementation

void updateState<T>(dynamic name,
    {dynamic data, dynamic Function(T? currentValue)? setValue}) {
  EventBus? eventBus = Backpack.instance.read("event_bus");
  if (eventBus == null) {
    NyLogger.error(
        'Event bus not defined. Please ensure that your project has called nylo.addEventBus() in one of your providers.');
    return;
  }

  dynamic dataUpdate = data;
  if (setValue != null) {
    List<EventBusHistoryEntry> eventHistory = eventBus.history
        .where(
            (element) => element.event.runtimeType.toString() == 'UpdateState')
        .toList();
    if (eventHistory.isNotEmpty) {
      T? lastValue = eventHistory.last.event.props[1] as T?;
      dataUpdate = setValue(lastValue);
    }
  }

  String stateName = '';
  if (name is String) {
    stateName = name;
  }
  if (name is RouteView) {
    stateName =
        "${name.$2.runtimeType.toString().replaceAll("BuildContext", "")}State"
            .replaceAll("() => ", "() => _");
  }

  final event = UpdateState(data: dataUpdate, stateName: stateName);
  eventBus.fire(event);
}