Last active
March 17, 2025 12:59
-
-
Save tonis2/58d48e023357a06c660ce048ed21563d to your computer and use it in GitHub Desktop.
Flutter app state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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