1

我正在弄清楚如何显示已安装应用程序的列表及其名称和图标。代码在显示应用名称之前运行良好。这是正确的工作代码:

import 'package:flutter/material.dart';
import 'package:device_apps/device_apps.dart';
import 'dart:async';

class FirstScreen extends StatefulWidget{
  State<StatefulWidget> createState(){
    return _FirstScreen();
  }
}

class _FirstScreen extends State<FirstScreen>{
  List<Application> apps;
  void initState(){
    super.initState();
  }

  Future<void> getApp() async{
    List<Application> _apps = await DeviceApps.getInstalledApplications(onlyAppsWithLaunchIntent: true, includeAppIcons: true, includeSystemApps: true);
    setState(() {
      apps = _apps;
    });
  }

  Widget build(BuildContext context) {
    getApp();
    return Container(
        child: ListView.builder(
          itemCount: apps.length,
          itemBuilder: (context, index){
            return ListTile(
              title: Text(apps[index].appName),
            );
          },
        )
    );
  }
}

但是当我通过以下方式在 ListTile 中显示应用程序图标时:

trailing: Icon(Image.memory(apps[index].icon))

它给出了未定义图标的错误。

我什至尝试了ApplicationWithIcon类,它扩展了其中定义的 Application 类和图标,但它返回了Null 错误

4

2 回答 2

1

而不是写

List<Application> apps

List apps

也,而不是写

List<Application> _apps = await DeviceApps.getInstalledApplications(onlyAppsWithLaunchIntent: true, includeAppIcons: true, includeSystemApps: true);

写:

 List _apps = await DeviceApps.getInstalledApplications(onlyAppsWithLaunchIntent: true, includeAppIcons: true, includeSystemApps: true);

最后,而不是

Icon(Image.memory(apps[index].icon))

Image.memory(apps[index] is ApplicationWithIcon ? app.icon : null)
于 2020-06-15T09:47:05.843 回答
0

那是因为 Icon 小部件收到一个 Icons 小部件并且您正在传递一个图像

ej:Icon(Icons.home)

所以只需传递图像

trailing: Image.memory(apps[index].icon)

更新

您必须删除类型并初始化列表:

 List apps = [];

在你的方法中也删除类型

 Future<void> getApp() async{
    List _apps = await DeviceApps.getInstalledApplications(onlyAppsWithLaunchIntent: true, 
        includeAppIcons: true, includeSystemApps: true);
      
       setState(() {
         apps = _apps;
      });
  }

于 2019-12-27T14:56:56.903 回答