PsyBorg
PsyBorg

Reputation: 21

Get app icons of installed apps in Flutter from device_apps plugin

I was figuring out how to display list of installed apps along with their names & icons. Code works well till displaying app names. Here is the right, working code:

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),
            );
          },
        )
    );
  }
}

but when I display app icon in ListTile by:

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

it gives icon not defined error.

I even tried ApplicationWithIcon class which extends Application class & icon defined in it, but it returned Null Error

Upvotes: 2

Views: 3456

Answers (2)

Kimiya Zargari
Kimiya Zargari

Reputation: 367

instead of writing

List<Application> apps

write

List apps

also, instead of writing

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

write:

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

finally, instead of

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

write

Image.memory(apps[index] is ApplicationWithIcon ? app.icon : null)

Upvotes: 1

Itamar Garcia
Itamar Garcia

Reputation: 906

That's because Icon widget receive a Icons widget and you're passing a Images

ej: Icon(Icons.home)

so just pass the image

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

Update

you must delete the type and initialize the list:

 List apps = [];

In your method delete type too

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

Upvotes: 0

Related Questions