简体   繁体   English

NotifyListeners 未更新 Flutter 小部件

[英]Flutter Widget not updated by NotifyListeners

I have a radio button that doesn't update on Notify Listeners despite the groupValue is updated, the radio choice isn't updated.尽管 groupValue 已更新,但我有一个单选按钮不会在 Notify Listeners 上更新,单选按钮未更新。 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(
      ),
    );
  }
}

I realized I should use ChangeNotifierProvider instead of Provider我意识到我应该使用 ChangeNotifierProvider 而不是 Provider

 return MultiProvider(
      providers: [
        
        ChangeNotifierProvider<Post>(
          create: (context) => Post(),
        ),
        ChangeNotifierProvider<Content>(
          create: (context) => Content(),
        ),
       
      ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM