rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

Unhandled Exception: Instance of 'DependencyNotFoundException<PrefsSupport>' when read<>() from StateNotifier #24

Closed tbm98 closed 4 years ago

tbm98 commented 4 years ago

void main:

void main() => runApp(MultiProvider(providers: [
      Provider<PrefsSupport>(create: (_) => PrefsSupport()),
      StateNotifierProvider<MainNotifier, MainState>(
        create: (_) => MainNotifier(),
      )
    ], child: MyApp()));

my PrefsSupport:

class PrefsSupport {
  PrefsSupport() {
    _init();
  }

  static const String store_key = 'make_sleep_better';
  static const String delay_minute_key = 'delay_minute';
  static const String dark_mode = 'dark_mode';
  Future<SharedPreferences> _prefs;

  void _init() {
    _prefs = SharedPreferences.getInstance();
  }

  Future<bool> saveDarkMode(bool value) async {
    return (await _prefs).setBool(dark_mode, value);
  }

  Future<bool> getDarkMode() async {
    await Future.delayed(const Duration(seconds: 1));
    return (await _prefs).getBool(dark_mode) ?? false;
  }

  Future<bool> saveDelayMinute(int delayMinute) async {
    return (await _prefs).setInt(delay_minute_key, delayMinute);
  }

  Future<int> getDelayMinute() async {
    return (await _prefs).getInt(delay_minute_key) ?? 14;
  }
}

my state_notifier class

class MainNotifier extends StateNotifier<MainState> with LocatorMixin {
  MainNotifier() : super(const MainState()) {
    _fileStore = const FileStore();
    _prefsSupport = PrefsSupport();
    _initLoad();
  }

  PrefsSupport _prefsSupport;
  FileStore _fileStore;

  void _initLoad() async {
    var test = await read<PrefsSupport>().getDarkMode(); // error in this line

    print('test is $test');
    final darkMode = await _prefsSupport.getDarkMode();
    state = MainState(darkMode: darkMode);
  }
....
rrousselGit commented 4 years ago

Instead of the constructor, you should initialize your StateNotifier inside its "initState" method

tbm98 commented 4 years ago

I found initState in mixin LocatorMixin, but if I don't use LocatorMixin then where should I put initialize function to ?

rrousselGit commented 4 years ago

If you don't use LocatorMixin, then you don't have access to read, therefore cannot have a DependencyNotFoundException. So the constructor is fine.

tbm98 commented 4 years ago

Thanks!

tbm98 commented 4 years ago

But why you don't put LocatorMixin into StateNotifier by default ?

rrousselGit commented 4 years ago

LocatorMixin is specific to provider/Flutter

StateNotifier isn't

On Fri, Jun 19, 2020, 04:30 tbm98 notifications@github.com wrote:

But why you don't put LocatorMixin into StateNotifier by default ?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/rrousselGit/state_notifier/issues/24#issuecomment-646415568, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEZ3I3JP2JV4MVACTJVEBMTRXLLWJANCNFSM4OCJGPOQ .

tbm98 commented 4 years ago

Thanks

Vào 10:48 Th 6, 19 thg 6 2020 Remi Rousselet notifications@github.com đã viết:

LocatorMixin is specific to provider/Flutter

StateNotifier isn't

On Fri, Jun 19, 2020, 04:30 tbm98 notifications@github.com wrote:

But why you don't put LocatorMixin into StateNotifier by default ?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub < https://github.com/rrousselGit/state_notifier/issues/24#issuecomment-646415568 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AEZ3I3JP2JV4MVACTJVEBMTRXLLWJANCNFSM4OCJGPOQ

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rrousselGit/state_notifier/issues/24#issuecomment-646419483, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMRATJBGMDFEBCTYGAYTFVDRXLNYRANCNFSM4OCJGPOQ .