Hi everyone I'm really new to flutter and programming. I'm trying to create an android app launcher with flutter.
I looked everywhere but I can't find an answer to my problem.
I'm using the package: device_apps
So to retrieve the app list I'm using
List<Application> apps = await DeviceApps.getInstalledApplications();
How can I update my listview when I install/uninstall an app?
DeviceApps.getInstalledApplications()
returns a List<Application>
which we can use to draw our ListView
:
Widget _buildListView() {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
Application app = _apps[index];
return Column(
children: <Widget>[
ListTile(
onTap: () => DeviceApps.openApp(app.packageName),
title: Text('${app.appName} (${app.packageName})'),
),
Divider()
],
);
},
itemCount: _apps.length);
}
We could load the installed applications immediately after initializing the state by overriding the initState
method of a StatefulWidget
:
List<Application> _apps = [];
@override
void initState() {
super.initState();
_loadApps();
}
Future<void> _loadApps() async {
List<Application> applications = await DeviceApps.getInstalledApplications();
setState(() => _apps = applications);
}
Widget _buildListView() {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
Application app = _apps[index];
return Column(
children: <Widget>[
ListTile(
onTap: () => DeviceApps.openApp(app.packageName),
title: Text('${app.appName} (${app.packageName})'),
),
Divider()
],
);
},
itemCount: _apps.length);
}
In this way, we could also easily use a GestureDetector
to reload the installed applications:
Widget _buildRefreshButton() {
return GesetureDetector(
onTap: () async { await _loadApps(); }
child: Text("Reload apps")
);
}
The best solution would be to get notified whenever an app is uninstalled/installed, but unfortunately, this function is not yet supported by the package.
An hacky workaround would be to poll the _loadApps()
above by the means of a Timer.periodic
, however, it is not recommended for performance and UX reasons.