json - 在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'}
解决方案
您可以使用以下代码从应用程序中提取 appName 并列出它。您可以直接编写该列表如果您想要字符串格式的 JSON 数组,那么您可以使用jsonEncode()
var appNamesList = apps.map((app)=>app.appName).toList();
推荐阅读
- android - 有什么方法可以在更改之前在回收站视图中操作项目
- mysql - 日期 + 3 个月间隔 mysql 列
- knockout.js - Knockout js在第二次绑定后保持显示/隐藏元素
- php - Solr 将索引的 Documents 值存储在数组中
- azure - 如何使用 PowerShell 验证 Azure 资源命名约定
- java - 什么是 TypeScript 等价于 ParentClass.this
- sql - 使用 sql server 将行转换为列
- go - 无法使用 sudo 命令运行 go 程序
- sass - 如何在 vue js 组件中使用 scss 文件?
- sockets - gRPC 套接字在具有入口的 kubernetes 上关闭