[英]Convert forEach List into json list in flutter
我正在嘗試獲取已安裝應用程序的列表,並能夠使用提到的代碼獲取它。 如何將此 forEach 信息放入 Json 列表以將其作為 json 保存到服務器
Future<void> getinstalledAppList() async{
List<Application> apps = await DeviceApps.getInstalledApplications();
apps.forEach((app) {
var jsonList = app.appName;
print(jsonList);
});
}
Output打印如下:
I/flutter ( 5465): WooCommerce
I/flutter ( 5465): Amazon
I/flutter ( 5465): Weather
I/flutter ( 5465): OfficeSuite
我需要 output,例如:
{'WooCommerce', 'Amazon', 'Weather' , 'OfficeSuite'}
您可以使用以下代碼從應用程序中提取 appName 並列出它。 您可以直接編寫該列表如果您想要字符串格式的 JSON 數組,那么您可以使用jsonEncode()
var appNamesList = apps.map((app)=>app.appName).toList();
您可以定義一個全局應用名稱列表。 並將項目推入此列表。 之后,您可以輕松地將其發送到服務器。 它看起來像這樣:
List<String> appNameList = List<String>();
Future<void> getinstalledAppList() async {
List<Application> apps = await DeviceApps.getInstalledApplications();
apps.forEach((app) {
var jsonList = app.appName;
appNameList.add(app.appName); // Here we add them to a string list.
print(jsonList);
});
}
Future<void> callThisFunction(List<String> appNameList) async {
// Here we can send the list to server
await saveAppNameListToServer(appNameList);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.