I'm new to riverpod. I'm trying to use the code generation API to create something that is like a state provider but I can't seem to create a usable instance of the provider.
This is my provider
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'main_mode_provider.g.dart';
enum EditorMode {
newFile,
editFile,
}
@riverpod
class MainModeProvider extends _$MainModeProvider {
@override
EditorMode build() {
return state;
}
void setEditorMode(EditorMode newMode) {
state = newMode;
}
}
final mainModeProvider = MainModeProvider();
The above is an a separate dart file.
In my main.dart
I import this the provider dart file and try to watch a ref of it but am getting an error.
class MyHomePage extends ConsumerWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
Widget page;
EditorMode mode = ref.watch(mainModeProvider);
and I'm getting the following error on the last line
The argument type 'MainModeProvider' can't be assigned to the parameter type 'ProviderListenable<EditorMode>'.
What am I doing wrong here? The code generation runs correctly.
return state;
You cannot "return state" in your build(), because the whole point of build() is to initialize state!
Also, because you named the class MainModeProvider
, the name of your provider will be mainModeProviderProvider
. Might want to dial that back a bit.