zbk
zbk

Reputation: 249

Flutter: How to rebuild using Riverpod within showModalBottomSheet

@override
  Widget build(BuildContext context, WidgetRef ref) {
    final provider = counterNotifierProviders(Counter.initialize());
    final counter = ref.watch(provider);
  
  return Scaffold(
    body: ElevatedButton(
      onPressed: () => showModalBottomSheet<void>(
        context: context,
          builder: (context) {
            return Column(
              children: [
                Text(counter.count),
                TextButton(child: 'Add' onPressed: () => ref.read(provider.notifier).add()),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Text(counter.count) is not updated. But, I know counter.count was updating from log.

How I can resolve that problem?

Upvotes: 5

Views: 2117

Answers (1)

Ayoub BOUMZEBRA
Ayoub BOUMZEBRA

Reputation: 5023

You can solve your problem by using Consumer to use its ref and rebuild the widget for you:

Consumer(
        builder: (BuildContext context, WidgetRef ref, Widget? child) {
     return Scaffold(...);
});

Upvotes: 0

Related Questions