Skip to content

Instantly share code, notes, and snippets.

@tonis2
Last active March 17, 2025 12:59
Show Gist options
  • Save tonis2/58d48e023357a06c660ce048ed21563d to your computer and use it in GitHub Desktop.
Save tonis2/58d48e023357a06c660ce048ed21563d to your computer and use it in GitHub Desktop.
Flutter app state
// CREATE STATE HERE
class Inherited extends InheritedNotifier<AppState> {
const Inherited({required super.child, super.key, required super.notifier});
static AppState? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<Inherited>()!.notifier;
}
@override
bool updateShouldNotify(InheritedNotifier<AppState> oldState) {
return true;
}
}
//BASE STATE CLASS, KEEP APPLICATION DATA IN HERE
class AppState extends ChangeNotifier {
User? user;
void login() async {
user = await serverRequest.getUser();
notifyListeners();
}
}
// BASE RENDER WIDGET
class Main extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Inherited(
notifier: AppState(),
child: router(),
);
}
}
// RUN FLUTTER LIKE THIS
runApp(Material(
child: Main(),
));
// QUERY STATE IN RANDOM WIDGETS, NEED TO BE NESTED UNDER MAIN
AppState provider = Inherited.of(context)!;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment