chetan suri
chetan suri

Reputation: 399

Print and save data of list of installed apps in flutter

I am trying to get list of appName for all Apps installed and using package: https://pub.dev/packages/device_apps . How to run this in initstate so I can run it in background and save data in backend. Below code prints all information while I am only looking for specific fields as list.


 void initState() {
    super.initState();
    getinstalledAppList();
  }
    Future<void> getinstalledAppList() async{
      List<Application> apps = await DeviceApps.getInstalledApplications();
      print(apps);

    }

Upvotes: 1

Views: 650

Answers (2)

Jhakiz
Jhakiz

Reputation: 1609

chetan suri you can map your apps list to new one or use foreach statement. Here is example:

    void initState() {
        super.initState();
        getinstalledAppList();
      }
        Future<void> getinstalledAppList() async{
          List<Application> apps = await DeviceApps.getInstalledApplications();
          print(apps);
          // Using foreach statement
          apps.forEach((app) {
              print(app.appName);
              // TODO Backend operation
          });
    
        }

Map apps list to new:

Class model:

class AppInfo {
  String appName, packageName, versionName;

  AppInfo({
    this.appName,
    this.packageName,
    this.versionName,
  });

  static List<AppInfo> retrieveSomeFields(List<Application> data) {
    return data
        .map(
          (app) => AppInfo(
            appName: app.appName,
            packageName: app.packageName,
            versionName: app.versionName,
          ),
        )
        .toList();
  }
}

Call:

Future<void> getinstalledAppList() async{
    List<Application> apps = await DeviceApps.getInstalledApplications();
    print(apps);
    var data = AppInfo.retrieveSomeFields(apps);
    // TODO Backend operation
}

Upvotes: 3

Akif
Akif

Reputation: 7650

You can write a work manager and callbackDispatcher for background processes. Here is a good explanation. It will look like this:

const myTask = "syncWithTheBackEnd";

void main() {
  Workmanager.initialize(callbackDispatcher);
  Workmanager.registerOneOffTask(
    "1",
    myTask, //This is the value that will be returned in the callbackDispatcher
    // Set Your Delay!
    initialDelay: Duration(minutes: 5),
    constraints: WorkManagerConstraintConfig(
      requiresCharging: true,
      networkType: NetworkType.connected,
    ),
  );
  runApp(MyApp());
}

void callbackDispatcher() {
  Workmanager.executeTask((task) {
    switch (task) {
      case myTask:
        print("this method was called from native!");
        // Call your own method for Android.
        getinstalledAppList();
        break;
      case Workmanager.iOSBackgroundTask:
        print("iOS background fetch delegate ran");
        // Call your own method for iOS.
        getinstalledAppList();
        break;
    }

    //Return true when the task executed successfully or not
    return Future.value(true);
  });
}

Upvotes: 0

Related Questions