检查项目是否有价值 'required'
Checking if item have value 'required'
所以我做了一个调查表,data/question 来自 api 每个数据项都有键 'required' 如果问题答案还没有填写按钮去下一页不可用。如何为它设置 if else
条件?
The form
The data
{
"success": true,
"msg": "OK",
"data": [
{
"id": 4,
"label":
"Is this a test ride period or demonstration period in this PoC?",
"required": true,
"options": [
{"label": "Test ride", "value": "1"},
],
},
{
"id": 3,
"label": "Have you answered this survey before?",
"required": true,
"options": [
{"label": "No, for the first time", "value": "1"},
],
}
]
},
一种方法是使用来自选项的小部件的回调。
class Data {
int id; // Will be unique
String label;
bool requireds;
List<Map<String, String>> options;
bool added; // Add an extra parameter in the class to manage if the value is selected or not.
Data({
required this.id,
required this.label,
required this.requireds,
required this.options,
this.added = false, // Set the default value as false
});
}
// Contains the list of data objects
List<Data> listData = [...];
// Callback from widgets to the parent widget
void updateData(bool isAdded, int id) {
listData.firstWhere((element) => element.id == id).added = true; // Update the added bool to true whenever a value is selected for the widget
}
// On click condition, can be added to state updater as well to make
// the button disabled or enabled
void checkConditions() {
List<Data> filter = [];
filter.addAll(listData);
// Filter the list for the condition
filter.retainWhere((data){
return data.requireds && !data.added; // If required and not added
});
// If the filter list is empty then all the required fields are answered
if (filter.isEmpty) {
// Go to next page or do an operation
} else {
// Do operation based on filter list values
}
}
这是我在 dart 中编写的代码片段,尚未在 Flutter 项目中尝试过。让我知道这是否有帮助或您遇到其他问题。
所以我做了一个调查表,data/question 来自 api 每个数据项都有键 'required' 如果问题答案还没有填写按钮去下一页不可用。如何为它设置 if else
条件?
The form
The data
{
"success": true,
"msg": "OK",
"data": [
{
"id": 4,
"label":
"Is this a test ride period or demonstration period in this PoC?",
"required": true,
"options": [
{"label": "Test ride", "value": "1"},
],
},
{
"id": 3,
"label": "Have you answered this survey before?",
"required": true,
"options": [
{"label": "No, for the first time", "value": "1"},
],
}
]
},
一种方法是使用来自选项的小部件的回调。
class Data {
int id; // Will be unique
String label;
bool requireds;
List<Map<String, String>> options;
bool added; // Add an extra parameter in the class to manage if the value is selected or not.
Data({
required this.id,
required this.label,
required this.requireds,
required this.options,
this.added = false, // Set the default value as false
});
}
// Contains the list of data objects
List<Data> listData = [...];
// Callback from widgets to the parent widget
void updateData(bool isAdded, int id) {
listData.firstWhere((element) => element.id == id).added = true; // Update the added bool to true whenever a value is selected for the widget
}
// On click condition, can be added to state updater as well to make
// the button disabled or enabled
void checkConditions() {
List<Data> filter = [];
filter.addAll(listData);
// Filter the list for the condition
filter.retainWhere((data){
return data.requireds && !data.added; // If required and not added
});
// If the filter list is empty then all the required fields are answered
if (filter.isEmpty) {
// Go to next page or do an operation
} else {
// Do operation based on filter list values
}
}
这是我在 dart 中编写的代码片段,尚未在 Flutter 项目中尝试过。让我知道这是否有帮助或您遇到其他问题。