Hey im building an app (I cant go deep into the functionality), im using hive as my local database and riverpod as my state management tool.
i just want to store appsettings in my local database and access them through riverpod but it seems like i cant do that.
here is my main.dart file which displays an empty scaffold or Onbording screeen based on use login variable in box named as 'mybox'
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:prototype/data.dart/settings.dart';
import 'package:prototype/onboarding/onboardingscreen.dart';
import 'package:prototype/providers/settingsprovider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter(AppSettingsAdapter());
await Hive.openBox<AppSettings>('mybox');
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Consumer(
builder: (context, ref, child) {
final settingsWatcher = ref.watch(settingsProvider);
return settingsWatcher.loggedIn
? Scaffold()
: OnboardingScreen();
},
),
theme: ThemeData(useMaterial3: true),
);
}
}
here is my settings provider
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive/hive.dart';
import 'package:prototype/data.dart/settings.dart';
final settingsProvider =
StateNotifierProvider<AppSettingsNotifier, AppSettings>(
(ref) => AppSettingsNotifier(AppSettings()),
);
class AppSettingsNotifier extends StateNotifier<AppSettings> {
AppSettingsNotifier(super._state) {
_loadInitialState();
}
Future<void> _loadInitialState() async {
if (_settingsBox.isNotEmpty) {
state = _settingsBox.getAt(0)!;
} else {
await _settingsBox.add(state);
}
}
final Box<AppSettings> _settingsBox = Hive.box('mybox');
// Future<void> _loadBox() async {
// if (_settingsBox.isEmpty) {
// await _settingsBox.add(AppSettings());
// }
// state = _settingsBox.getAt(0)!;
// }
Future<void> updateDarkMode(bool darkMode) async {
await _loadInitialState();
final updatedSettings = state.copyWith(darkMode: darkMode);
await _settingsBox.putAt(0, updatedSettings);
state = updatedSettings;
}
Future<void> updateLoginStatus(bool loggedIn) async {
await _loadInitialState();
final updatedSettings = state.copyWith(loggedIn: loggedIn);
await _settingsBox.putAt(0, updatedSettings);
state = updatedSettings;
print("Logged in $loggedIn");
}
}
and this problem is definetly my skill issue i cant figure out how to so this
when i try to run the code i encounter this (in picture)
i tried giving this to chatgpt, qwen, deepseek everything failed to solve the problem the chatgpt is even told me not to use await when adding into box even though it is a future
and i have figured out somethings in that there is no error but the time is not updating to scaffold the blank one (whenever ichange the login status), it will updating to blank one when i reopen the app, unfortunately i dont have the code at the moment
yeah it seems i dont know anything to try
if i cant do this can you suggest me a alternate local database or state management tool ?
im completely open to any advices