I am working with riverpod
and flutter_hooks
I am trying to get implement shared_prefrences
using this 2 approaches and this is what I get to so far :
pref_changenotifier.dart
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _isloggedkey = 'logged';
const _boardingkey = "boarding";
const _apiTokenkey = 'apitoken';
const _rolekey = 'role';
class PrefChangeNotifier with ChangeNotifier {
late final SharedPreferences _sharedPreferences;
PrefChangeNotifier(this._sharedPreferences) {
getPref();
notifyListeners();
}
bool? _islogged;
bool? _boarding;
String? _apitoken;
String? _role;
String get apitoken => _apitoken!;
String get role => _role!;
bool get islogged => _islogged!;
bool get boarding => _boarding!;
void getPref() async {
_islogged = _sharedPreferences.getBool(_isloggedkey);
_apitoken = _sharedPreferences.getString(_apiTokenkey);
_boarding = _sharedPreferences.getBool(_boardingkey);
notifyListeners();
}
void setLoggedValue(bool logged) async {
await _sharedPreferences.setBool(_isloggedkey, logged);
_islogged = logged;
notifyListeners();
}
void setTokenValue(String apitoken) async {
await _sharedPreferences.setString(_apiTokenkey, apitoken);
_apitoken = apitoken;
notifyListeners();
}
void setBoardingValue(bool boarding) async {
await _sharedPreferences.setBool(_boardingkey, boarding);
_boarding = boarding;
notifyListeners();
}
void setRoleValue(String role) async {
await _sharedPreferences.setString(_rolekey, role);
_role = role;
notifyListeners();
}
void clearAllValues() async {
await _sharedPreferences.clear();
notifyListeners();
}
}
pref_provider.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'pref_changenotifier.dart';
import 'package:shared_preferences/shared_preferences.dart';
final sharedPreferences = FutureProvider<SharedPreferences>(
(ref) async => await SharedPreferences.getInstance());
final prefChangeNotifierProvider =
ChangeNotifierProvider<PrefChangeNotifier>((ref) {
final sharePreferencesData = ref.watch(sharedPreferences).asData;
late final SharedPreferences prefs;
if (sharePreferencesData != null) {
prefs = sharePreferencesData.value;
}
return PrefChangeNotifier(prefs);
});
But whenever I call it using : ref.read(prefChangeNotifierProvider).setBoardingValue(true);
, it throws this error
E/flutter (22947): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: An exception was thrown while building ChangeNotifierProvider<PrefChangeNotifier>#05878.
E/flutter (22947):
E/flutter (22947): Thrown exception:
E/flutter (22947): An exception was thrown while building _NotifierProvider<PrefChangeNotifier>#3bb8a.
E/flutter (22947):
E/flutter (22947): Thrown exception:
E/flutter (22947): LateInitializationError: Local 'prefs' has not been initialized.
E/flutter (22947):
E/flutter (22947): Stack trace:
i know that the problem is with my late initialization but what should i do ?