How to pass a parameter to a Mobx Controller (Flutter)

1,205

What you need is to declare constructor for CreateBillController instead of _CreateBillControllerBase, because constructor is not inherited by child class in Dart. The simplest way is to assign the passed in datasource to the corresponding property in parent class in the constructor, as you can see in the below snippet. You can also implement a constructor for _CreateBillControllerBase as well and call super(datasource) in CreateBillController's constructor.

import 'package:mobx/mobx.dart';

part 'create_bill_controller.g.dart';

class CreateBillController extends _CreateBillControllerBase with _$CreateBillController {
  // HERE! Implement constructor for CreateBillController
  // Do this if you have a constructor for _CreateBillControllerBase
  //
  //   CreateBillController(DataSource datasource) : super(datasource)
  //
  CreateBillController(DataSource datasource) {
    super.datasource = datasource;
  }
}

abstract class _CreateBillControllerBase with Store {
  final appBarTitle = 'Criar Conta';
  final criarConta = 'Criar conta';
  final nomeDaConta = 'Nome da conta';
  final seuNome = 'Seu nome';
  // HERE! Declare datasource
  DataSource datasource;

  @action
  createBill(String billname, String userName) {
    datasource.createBill(billName, userName);
  }
}
Share:
1,205
Guilherme Oliveira
Author by

Guilherme Oliveira

Trying to find a good coding tattoo.

Updated on December 23, 2022

Comments

  • Guilherme Oliveira
    Guilherme Oliveira over 1 year

    I am an Android Developer and new to Flutter. I really like the way Mobx works, because it remembers me Android's ViewModel. By the way, when I create a ViewModel, I like to create it passing the repository as a parameter, so I can test it with different Data Sources (ie. local or cloud).

    So, this is my class right now.

    import 'package:mobx/mobx.dart';
    
    part 'create_bill_controller.g.dart';
    
    class CreateBillController = _CreateBillControllerBase
        with _$CreateBillController;
    
    abstract class _CreateBillControllerBase with Store {
      final appBarTitle = 'Criar Conta';
      final criarConta = 'Criar conta';
      final nomeDaConta = 'Nome da conta';
      final seuNome = 'Seu nome';
    
      @action
      createBill(String billname, String userName) {
        // here, dataSource should be given in a constructor
        datasource.createBill(billName, userName);
      }
    }
    

    How can I pass a DataSource (repository) as a parameter to this class?

    • Danila
      Danila over 3 years
      I am not 100% sure about it, but if Dart is anything close to JS then you probably need to use constructor function? And pass all dynamic stuff through it dart.dev/guides/language/language-tour#constructors
    • Guilherme Oliveira
      Guilherme Oliveira over 3 years
      Hi, @Danila. I thought that too, but I am using MobX and it did not work when I tried to use a constructor inside the abstract class.