首页 > 解决方案 > 在flutter中将forEach List转换为json列表

问题描述

我正在尝试获取已安装应用程序的列表,并能够使用提到的代码来获取它。如何将此 forEach 信息放入 Json 列表以将其作为 json 保存到服务器

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

    apps.forEach((app) {
      var jsonList = app.appName;
      print(jsonList);
    });

  }

打印输出如下:

I/flutter ( 5465): WooCommerce
I/flutter ( 5465): Amazon
I/flutter ( 5465): Weather
I/flutter ( 5465): OfficeSuite

我需要像这样的输出:

{'WooCommerce', 'Amazon', 'Weather' , 'OfficeSuite'}

标签: jsonflutter

解决方案


您可以使用以下代码从应用程序中提取 appName 并列出它。您可以直接编写该列表如果您想要字符串格式的 JSON 数组,那么您可以使用jsonEncode()

var appNamesList = apps.map((app)=>app.appName).toList();

推荐阅读