[英]Flutter function error: Column's children must not contain any null values, but a null value was found at index 1
[英]Column's children must not contain any null values, but a null value was found at index 13. Flutter Function
我開發了一款提醒老年人服葯時間的應用程序。 在這里他們必須給他們服葯的劑量,比如一天一次和一天兩次。 我創建了一個 function ,它采用這個劑量並返回字段等於這些時間。 例如,如果用戶 select '每天三次',則會出現 3 個字段到 select 時間。
當我運行我的應用程序時,出現以下錯誤:
Column's children must not contain any null values, but a null value was found at index 13
我發現錯誤是由這個 function 引起的:
Widget time(String value) {
Widget w;
if (value == "مرة واحدة في اليوم"){
w = SizedBox(
height: 1,
);}
else if (value == 'مرتان في اليوم') {
w = AddContainer(
text: DateFormat('hh:mm').format(updatedTime2), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context, builder: buildShowModalBottomSheetMedTime2);
});
} else if (value == "ثلاث مرات في اليوم") {
w = Column(
children: [
AddContainer(
text: DateFormat('hh:mm').format(updatedTime2), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildShowModalBottomSheetMedTime2);
}),
SizedBox(height: 20),
AddContainer(
text: DateFormat('hh:mm').format(updatedTime3), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildShowModalBottomSheetMedTime3);
}),
SizedBox(height: 20),
],
);
} else if (value == "اربعة مرات في اليوم") {
w = Column(
children: [
AddContainer(
text: DateFormat('hh:mm').format(updatedTime2), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildShowModalBottomSheetMedTime2);
}),
SizedBox(height: 20),
AddContainer(
text: DateFormat('hh:mm').format(updatedTime3), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildShowModalBottomSheetMedTime3);
}),
SizedBox(height: 20),
AddContainer(
text: DateFormat('hh:mm').format(updatedTime4), // ("وقت الدواء"),
onPressed: () {
showModalBottomSheet(
context: context,
builder: buildShowModalBottomSheetMedTime4);
}),
SizedBox(height: 20),
],
);
}
return w;
}
我怎么解決這個問題?
您可以替換所有出現的 dateFormat ,例如:
text: DateFormat('hh:mm').format(updatedTime2),
經過
text: updatedTime2 != null ? DateFormat('hh:mm').format(updatedTime2) : "",
您不能將 null 值賦予文本。
這將允許您的小部件工作,直到用戶 select 一次。
那是因為當所有if
檢查都不匹配時,您沒有默認值。
您需要為此添加else
部分。 像這樣的東西:
Widget time(String value) {
Widget w;
if (value == "مرة واحدة في اليوم"){
w = SizedBox(
height: 1,
);
} else if (...) {
///
} else {
// Add your widget here if all the `if` not matching
w = Text('No matching');
}
return w;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.