Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Locale property

Tryed to make the Provider LocaleProvider. But get the Error "Non-nullable instance field ‘_locale’ must be initialized.
Try adding an initializer expression, or a generative constructor that initializes it, or mark it ‘late’." making it "late" gets me the error "The following LateError was thrown building Builder(dirty, dependencies: [_InheritedProviderScope]):
LateInitializationError: Field ‘_locale@23001738’ has not been initialized."

class LocaleProvider extends ChangeNotifier {
  Locale _locale;

  Locale get locale => _locale;

  void setLocale(Locale locale) {
    if (!L10n.all.contains(locale)) return;

    _locale = locale;
    notifyListeners();
  }

  void clearLocale() {
    _locale = const Locale('en');
    notifyListeners();
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Add a constructor

class LocaleProvider extends ChangeNotifier {
  Locale _locale;

  LocaleProvider(this._locale);//constructor for field initialization

  Locale get locale => _locale;

  void setLocale(Locale locale) {
    if (!L10n.all.contains(locale)) return;

    _locale = locale;
    notifyListeners();
  }

  void clearLocale() {
    _locale = const Locale('en');
    notifyListeners();
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading