android - 无法在Flutter中设置SetState()

标签 android flutter dart

AppScreenC将以ListView提供所有已安装的应用程序

class AppScreenC extends StatefulWidget {
  @override
  _AppScreenCState createState() => _AppScreenCState();
}

List<Application> apps;

getApps() async {
  if (apps == null) {
    apps = await DeviceApps.getInstalledApplications(
        onlyAppsWithLaunchIntent: true,
        includeSystemApps: true,
        includeAppIcons: true);
    apps.sort((a, b) => a.appName.compareTo(b.appName));
  }
}
屏幕上将显示具有所有已安装应用程序的ListView。我正在尝试根据onTap事件更改图标。
但是,单击列表后,图标没有改变。
class _AppScreenCState extends State<AppScreenC> {
  final _app = <Application>[];
  @override
  Widget build(BuildContext context) {
    return _buildApps();
  }

  Widget _buildApps() {
    getApps();
    return ListView.builder(itemBuilder: (BuildContext context, int index) {
      _app.addAll(apps);
      return _buildRow(_app[index]);
    });
  }

  Widget _buildRow(ApplicationWithIcon app) {
    bool selected = false;
    return ListTile(
      leading: Image.memory(app.icon, height: 40),
      trailing:
          Icon(selected ? Icons.check_circle : Icons.check_circle_outline),
      title: Text(app.appName),
      onTap: () {
        selected = !selected;
        // print("$selected");
        // print("${app.apkFilePath}");
        setState(() {});
      },
    );
  }
}

最佳答案

itemCount:中缺少ListView.Builder

   Widget _buildApps() {
        getApps();
        return ListView.builder(itemBuilder: (BuildContext context, int index) {
          _app.addAll(apps);
          return _buildRow(_app[index]);
        }, itemCount: _app.length);
     }
也,
class _AppScreenCState extends State<AppScreenC> {
bool selected = false; // this should be at the top as it will persist the value

关于android - 无法在Flutter中设置SetState(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63682102/

相关文章:

android - ImageView 未覆盖父 CardView 的完整宽度

flutter - 如何在Google上创建类似于 'my activity'的用户历史记录页面-Flutter

react-native - 使用 Ionic、React Native、NativeScript 和 Flutter 捕获 "call end"事件?

flutter - 如何使用 Dart/flutter 将数字格式化为数千、数百万和数十亿?

android - 在已创建父 fragment View 后添加嵌套 fragment

android - 发布版本中的空 json 数据(使用混淆器)

4G 安卓 FTP

Flutter Riverpod - 使用异步值初始化

dart - Angular-dart 组件并不总是显示在网络应用程序中

dart - Flutter - 底部溢出 119 像素