Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tbm98 opened this issue Jun 19, 2020 · 7 comments

Comments

@tbm98
Copy link
Contributor

tbm98 commented Jun 19, 2020

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
Copy link
Owner

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

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 19, 2020

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

@rrousselGit
Copy link
Owner

rrousselGit commented Jun 19, 2020

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
Copy link
Contributor Author

tbm98 commented Jun 19, 2020

Thanks!

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 19, 2020

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

@rrousselGit
Copy link
Owner

rrousselGit commented Jun 19, 2020 via email

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 19, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants