Pragmatic State Handling in Flutter | by Erlend | Feb, 2022

Which state administration answer works finest for you?

Picture by NordWood Themes on Unsplash

Primary state

class UserState extends ValueNotifier<Person?> 
UserState(Person? consumer) : tremendous(consumer);
remaining userState = UserState(null);

Extra complicated state


class UserState extends ValueNotifier<Person?>
UserState(Person? consumer) : tremendous(consumer)
FirebaseAuth.occasion.userChanges().hear((Person? consumer)
worth = consumer;
);

Pay attention for state modifications

@override
Widget construct(BuildContext context)
return ValueListenableBuilder<Person?>(
valueListenable: userState,
builder: (context, Person? consumer, baby)
// TODO..

);
userState.addListener(()  
Person consumer? userState.worth;
// TODO..
);

Pay attention for a number of state modifications

return AnimatedBuilder(
animation: Listenable.merge([
userState,
someOtherState,
]),
builder: (context, baby) {
remaining Person? consumer = userState.worth;
remaining OtherState? otherValue = someOtherState.worth;

Preload state

class SomeState lengthen ValueNotifier 
Future<void> init() async
// TODO do async work and return

dwelling: FutureBuilder(
future: Future.wait([
userState.init(),
otherState.init(),
]),
builder: (context, snapshot)
// TODO ..
,
)

State metadata

class StateEvent<T> 
remaining T? information;
remaining bool loading;
remaining Object? error;
Wish to Join?Please get in contact on Twitter @apptakk you probably have any questions or feedback

More Posts