how can i change bool variable using Flutter Riverpod
210
The value in a Provider
cannot be changed from outside the Provider
. Use a StateProvider
instead.
Check this out:
final testProvider = StateProvider<bool>((ref) => false);
class FriendList extends ConsumerStatefulWidget {
const FriendList({Key? key}) : super(key: key);
@override
ConsumerState<ConsumerStatefulWidget> createState() => _FriendListState();
}
class _FriendListState extends ConsumerState<FriendList> {
@override
Widget build(BuildContext context) {
bool test = ref.watch(testProvider);
return IconButton(
onPressed: () {
ref.read(testProvider.state).state = true;
},
icon: Text(
test ? "Variable updated" : "Still not updated",
),
);
}
}
Visit Riverpod documentation to learn more about it.
EDIT: I have written a complete example using Provider
and StateNotifierProvider
A service to communicate with Firebase
class UserService {
static Future<Map> getUser() async {
DocumentSnapshot<Map> s = await FirebaseFirestore.instance
.collection('users')
.doc('userId')
.get();
return Map.from(s.data()!);
}
}
A StateNotifierProvider
and StateNotifier
communicating with the service:
final userProvider = StateNotifierProvider<UserNotifier, AsyncValue<Map>>((ref) => UserNotifier());
class UserNotifier extends StateNotifier<AsyncValue<Map>> {
UserNotifier() : super(AsyncLoading()) {
get();
}
void get() async {
state = AsyncData(await UserService.getUser());
}
}
A testProvider
final testProvider = Provider<bool>((ref) {
AsyncValue test = ref.watch(userProvider);
return test is AsyncData;
});
And the Provider
s consumption
class FriendList extends ConsumerWidget {
const FriendList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
AsyncValue value = ref.read(userProvider);
bool test = ref.read(testProvider);
return Column(
children: [
Text(test ? "bool varialbe is true" : "bool varialbe is false"),
value.when(
loading: () => Text('Loading...'),
data: (data) => Text('${data['name']}'),
error: (error, __) => Text('Error: $error'),
),
],
);
}
}
Author by
Nadia Yaseen
Updated on January 03, 2023Comments
-
Nadia Yaseen over 1 year
i am new to
Flutter Riverpod management
i have stfl widget using
Flutter Riverpod management
, i need to change the test bool varible to true but cannot handle itfinal testProvider = StateProvider<bool>((ref) => false); `class FriendsList extends ConsumerStatefulWidget { const FriendsList({Key? key}) : super(key: key); @override _FriendsListState createState() => _FriendsListState(); } class _FriendsListState extends ConsumerState<FriendsList> { @override Widget build(BuildContext context) { bool test = ref.watch(testProvider); return Stack( //edit children [ !test ? Text('bool varible is false') : Text('bool varible is true') FutureBuilder( future: FirebaseFirestore.instance.collection('users') .doc('userId').get(), builder: (context, AsyncSnapshot snapshot) { if(!snapshot.hasData){ return Text(''); }else{ ref.read(testProvider.state).state = true; } return snapshot.data['name'];
i have no idea how to handle this . also i am not sure of my code if it right , and how to dispose it if it need to
-
Nadia Yaseen over 2 yearsthanks it work , but it give warning message setState() or markNeedsBuild() called during build. however i don't use setstate
-
Josteve over 2 yearsHmm...It shouldn't, is this only place you used riverpod?
-
Josteve over 2 yearsAlso, can you please accept and upvote?
-
Nadia Yaseen over 2 yearsno difference place , please take a look again since i edit the code , in the fact i was using setstate before , then someone give me tip to use riverpod instead so i can avoid this warning message of memory leak , but now it is the same message , it like disappointed, this is simple sample ,
-
Nadia Yaseen over 2 yearsoh it you the one who give me that tip , well , it's same Error :D
-
Josteve over 2 yearsHey @NadiaYaseen check the edit.
-
Nadia Yaseen over 2 yearssame............
-
Josteve over 2 yearsHey @NadiaYaseen, I've added the complete code for the entire thing with Riverpod.