0

I have a radio button that doesn't update on Notify Listeners despite the groupValue is updated, the radio choice isn't updated. I use multiproviders.

Here is the Radio Choices

ListTile(
              leading: Icon(
                 Icons.edit_outlined,
              ),
              title: Text('Post now'),
              trailing: Radio<PostOption>(
                value: PostOption.now,
                groupValue: Provider.of<Post>(context).postOption,
                onChanged: (value) {
                  Provider.of<Post>(context, listen: false)
                      .choosePostOption(value);
    
                },
              ),
            ),
            ListTile(
              leading: Icon(
                Icons.drive_folder_upload,
              ),
              title: Text('Save as Draft'),
              trailing: Radio<PostOption>(
                value: PostOption.draft,
                groupValue: Provider.of<Post>(context).postOption,
                onChanged: (value) {
                  Provider.of<Post>(context, listen: false)
                      .choosePostOption(value);
            
                },
              ),
            ),

And Here is the provider class

class Post extends ChangeNotifier {
  PostOption postOption;

  void choosePostOption(PostOption option) {
    postOption = option;
    notifyListeners();
  }

}

Here is the main where I set up my providers

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        
        Provider<Post>(
          create: (context) => Post(),
        ),
        Provider<Content>(
          create: (context) => Content(),
        ),
       
      ],
      child: MaterialApp(
      ),
    );
  }
}
Ammar Mohamed
  • 513
  • 1
  • 6
  • 16

1 Answers1

0

I realized I should use ChangeNotifierProvider instead of Provider:

 return MultiProvider(
      providers: [
        
        ChangeNotifierProvider<Post>(
          create: (context) => Post(),
        ),
        ChangeNotifierProvider<Content>(
          create: (context) => Content(),
        ),
       
      ],
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ammar Mohamed
  • 513
  • 1
  • 6
  • 16