Search code examples
flutterdartcode-generationmobx

Why won't Flutter mobX store code generate?


I have a mobx store and it's really hit and miss getting my storename.g.dart generated.

The first time, I copied an example from medium and just kept running the generator until eventually it gave code.

The second time, I wrote my own store forgot the abstract keyword. Added it in and it worked.

I thought that was the problem.

Now, third project, I think I have all my code down but it's still not generating. Many actions but no output.

What makes it work or fail? This is my current mobx

import 'package:mobx/mobx.dart';
import 'package:firebase_auth/firebase_auth.dart';

// have this line
// then generate with
// flutter packages pub run build_runner build
// or
// flutter packages pub run build_runner clean; flutter packages pub run build_runner build --delete-conflicting-outputs

// todo edit this file name
part 'usermodel.g.dart';

class UserModel = UserModelBase with _$UserModel;

abstract class UserModelBase implements Store {
  @observable
  FirebaseUser user;

  @action
  setUser(FirebaseUser u){
    user = u;
  }

  dispose() {}
}

Solution

  • Store is should be used as a mixin.

    Do:

    abstract class Foo with Store {}
    

    Don't:

    abstract class Foo implements Store {
      void dispose() {} 
    }