[英]Flutter: How to map a Dictionary? Make dropdown options via dictionary
我需要一些抖動數據映射方面的幫助。 我有一個JSON對象,正在返回一些字段。 我必須根據這些字段創建一個表單。 我現在面臨的問題是我無法將JSON字典映射到我的下拉列表。
所以基本上我想在json的form_field_options中創建一個下拉選項。
這是我嘗試激活的代碼示例,服務器返回的JSON是:
{
"status": "200",
"request": "0",
"message": "Success",
"data": {
"assetID": "155",
"assetTitle": "TPO",
"formTitle": "Roof Asset",
"preFields": [
{
"unique_id": "uid_201955451258",
"form_name": "General Overview",
"form_field_type": "100",
"form_field_required": "0",
"form_field_private": "0",
"form_field_duplicate_times": "00",
"form_field_disabledrow": "0"
},
{
"unique_id": "uid_201939764918",
"form_name": "Asset ID",
"form_field_type": "5",
"form_field_required": "1",
"form_field_private": "0",
"form_field_duplicate_times": "00",
"form_field_disabledrow": "0"
},
{
"unique_id": "uid_201789014253",
"form_name": "Facility ID",
"form_field_type": "5",
"form_field_required": "0",
"form_field_private": "0",
"form_field_duplicate_times": "00",
"form_field_disabledrow": "0"
},
{
"unique_id": "uid_201996716360",
"form_name": "Location",
"form_field_type": "19",
"form_field_required": "0",
"form_field_private": "0",
"form_field_duplicate_times": "00",
"form_field_disabledrow": "0"
},
{
"unique_id": "uid_201941758250",
"form_name": "Developed Area Type",
"form_field_type": "1",
"form_field_required": "0",
"form_field_private": "0",
"form_field_duplicate_times": "00",
"form_field_options": {
"1": {
"opt_name": "Suburban",
"opt_weightage": ""
},
"2": {
"opt_name": "Urban",
"opt_weightage": ""
},
"3": {
"opt_name": "Rural",
"opt_weightage": ""
}
},
"form_field_disabledrow": "0"
}
]
}
}
這是我的表單類(自動生成類的表單quicktype)是:
// To parse this JSON data, do
//
// final form = formFromJson(jsonString);
import 'dart:convert';
Form formFromJson(String str) => Form.fromJson(json.decode(str));
String formToJson(Form data) => json.encode(data.toJson());
class Form {
String status;
String request;
String message;
Data data;
Form({
this.status,
this.request,
this.message,
this.data,
});
factory Form.fromJson(Map<String, dynamic> json) => new Form(
status: json["status"],
request: json["request"],
message: json["message"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"status": status,
"request": request,
"message": message,
"data": data.toJson(),
};
}
class Data {
String assetId;
String assetTitle;
String formTitle;
List<PreField> preFields;
Data({
this.assetId,
this.assetTitle,
this.formTitle,
this.preFields,
});
factory Data.fromJson(Map<String, dynamic> json) => new Data(
assetId: json["assetID"],
assetTitle: json["assetTitle"],
formTitle: json["formTitle"],
preFields: new List<PreField>.from(json["preFields"].map((x) => PreField.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"assetID": assetId,
"assetTitle": assetTitle,
"formTitle": formTitle,
"preFields": new List<dynamic>.from(preFields.map((x) => x.toJson())),
};
}
class PreField {
String uniqueId;
String formName;
String formFieldType;
String formFieldRequired;
String formFieldPrivate;
String formFieldDuplicateTimes;
String formFieldDisabledrow;
Map<String, FormFieldOption> formFieldOptions;
PreField({
this.uniqueId,
this.formName,
this.formFieldType,
this.formFieldRequired,
this.formFieldPrivate,
this.formFieldDuplicateTimes,
this.formFieldDisabledrow,
this.formFieldOptions,
});
factory PreField.fromJson(Map<String, dynamic> json) => new PreField(
uniqueId: json["unique_id"],
formName: json["form_name"],
formFieldType: json["form_field_type"],
formFieldRequired: json["form_field_required"],
formFieldPrivate: json["form_field_private"],
formFieldDuplicateTimes: json["form_field_duplicate_times"],
formFieldDisabledrow: json["form_field_disabledrow"],
formFieldOptions: json["form_field_options"] == null ? null : new Map.from(json["form_field_options"]).map((k, v) => new MapEntry<String, FormFieldOption>(k, FormFieldOption.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"unique_id": uniqueId,
"form_name": formName,
"form_field_type": formFieldType,
"form_field_required": formFieldRequired,
"form_field_private": formFieldPrivate,
"form_field_duplicate_times": formFieldDuplicateTimes,
"form_field_disabledrow": formFieldDisabledrow,
"form_field_options": formFieldOptions == null ? null : new Map.from(formFieldOptions).map((k, v) => new MapEntry<String, dynamic>(k, v.toJson())),
};
}
class FormFieldOption {
String optName;
String optWeightage;
FormFieldOption({
this.optName,
this.optWeightage,
});
factory FormFieldOption.fromJson(Map<String, dynamic> json) => new FormFieldOption(
optName: json["opt_name"],
optWeightage: json["opt_weightage"],
);
Map<String, dynamic> toJson() => {
"opt_name": optName,
"opt_weightage": optWeightage,
};
}
現在,當我嘗試將循環或映射應用於我的選項列表(如果列表中的列表運行得很好)時,它將不會映射它:
Column(
children: <Widget>[
FormBuilderDropdown(
attribute: item.uniqueId,
decoration:
InputDecoration(labelText: item.formName),
// initialValue: 'Male',
hint: Text(item.formName),
// validators: [
// FormBuilderValidators.required()
// ],
items: item.formFieldOptions.map((option) => DropdownMenuItem(
value: option,
child: Text("$option.optName")))
.toList(),
),
],
),
它引發了以下錯誤:
> The argument type '(String) → MapEntry<?, ?>' can't be assigned to the
> parameter type '(String, FormFieldOption) → MapEntry<dynamic,
> dynamic>'.dart(argument_type_not_assignable)
請幫助或讓我知道我在做什么錯。 我該如何選擇這些下拉菜單。
提前致謝
您嘗試使用正確的lambda簽名(k, v) =>
映射
items: item.formFieldOptions.map((key, option) => DropdownMenuItem(
value: key,
child: Text("${option.optName}"),
)
).toList()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.