提供程序未更新UI

时间:2020-10-04 12:46:14

标签: flutter dart flutter-provider

我有一个ListView。我需要根据用户点击来更改leading:的{​​{1}}属性。 UI不会因交互而改变。它在ListTile

之后更新

我有Hot Restart这样的设置

Appmodel

我有这样的class AppModel extends ChangeNotifier { final _saved = Set<String>(); Set<String> getApps() => _saved; addApp(String apkPath) { _saved.add(apkPath); ChangeNotifier(); } removeApp(String apkPath) { _saved.remove(apkPath); ChangeNotifier(); } }

main.dart

我的Widget build(BuildContext context) { return MultiProvider( providers: [ ChangeNotifierProvider<AppModel>( create: (_) => AppModel(), ), FutureProvider<List<Application>>( create: (_) => appService.fetchCountries(), ), ], child: MaterialApp( debugShowCheckedModeBanner: false, title: 'Countries List', theme: ThemeData(primaryColor: Colors.green), home: Home(), ), ); } 这样

ListView

1 个答案:

答案 0 :(得分:1)

如果需要通过notifyListeners()调用进行某些更改,则需要通知侦听器:

 class AppModel extends ChangeNotifier {
   final _saved = Set<String>();
   Set<String> getApps() => _saved;

   addApp(String apkPath) {
     _saved.add(apkPath);
     notifyListeners();
   }

   removeApp(String apkPath) {
     _saved.remove(apkPath);
     notifyListeners();
   }
 }