How to pass a data from dialog view to dialog controller

I need to load API data from controller with parms, but i have don't know how to pass the id from view to controller like arguments

Here is my dialog view:

class ProcessWashingDialog extends BaseWidget<ProcessWashingDialogController> {
  final int id; // the id i need to pass
  final String title;
  final VoidCallback onPressed;
  final VoidCallback onBackPressed;

  ProcessWashingDialog({
    required this.id,
    required this.title,
    required this.onPressed,
    required this.onBackPressed,
  }) : super(AppComponent.getInjector().get());

  @override
  Widget get view => ControlledWidgetBuilder<ProcessWashingDialogController>(
        builder: (context, controller) => Container(
          width: AppSize.DIMEN_324.h,
          height: AppSize.DIMEN_367.h,
          padding: EdgeInsets.all(AppSize.DIMEN_36.h),
          child: controller.isLoading
              ? Center(child: CommonLoading())
              : Column(...

and here is my dialog controller:

class ProcessWashingDialogController extends BaseController {
  ProcessWashingDialogPresenter _presenter;

  ProcessWashingDialogController(this._presenter);
  late TimerRegister _timerRegister;
  TimerRegister get timerRegister => _timerRegister;

  @override
  void initListeners() {
    super.initListeners();

    _initObserver();
    _getTimeRegister();
  }

  void _initObserver() {
    _presenter.onFailure = (e) {};
    _presenter.onFinish = dismissLoading;
    _presenter.onSuccess = (data) {
      _timerRegister = data ?? timerRegister;
    };
  }

  void _getTimeRegister() {
    showLoading();

    _presenter.getTimerRegister(
      <String, String>{
        'Filter[Id]': 104.toString(), // i need to put the variable id here
      },
    );
  }
}

Anyone please help me. Thank you!

Upvotes: 1

Views: 409

Answers (2)

I have completed with this question, i do like this:

view dialog:

class ProcessWashingDialog extends BaseWidget<ProcessWashingDialogController> {
  final int id;
  final String title;
  final VoidCallback onPressed;
  final VoidCallback onBackPressed;
  final ProcessWashingDialogController _controller;

  ProcessWashingDialog(
  this._controller,
  {
    required this.id,
    required this.title,
    required this.onPressed,
    required this.onBackPressed,
  }) : super(_controller) {
    _controller.setArguments(id);
  };

controller dialog:

void setArguments(int id) {
   _getTimeRegister(id);
}

Upvotes: 1

Souvenir
Souvenir

Reputation: 108

Method 1: you can write a function that returns the value you need and then retrieves it from the controller.

Method 2: You create an id variable in the controller and then get it straight from that controller.

class Controller {
  String id;

  void getData() {
    // call api
    id = data;
  }
}

View

Text(controller.id)

Method 3: You can callback with params

Class controller {
  Function(int) onSuccess;

  void getData() {
    // call api
    onSuccess(id);
  }
}

View

void onSuccess(int id) {
  // handle id
}

Upvotes: 0

Related Questions