首页 > 解决方案 > 列的子项不得包含任何空值,但在索引 13 处找到空值。 Flutter 函数

问题描述

我开发了一款提醒老年人服药时间的应用程序。在这里,他们必须选择药物剂量,例如一天一次和一天两次。我创建了一个函数,它接受这个剂量并返回等于这些时间的字段。例如,如果用户选择“一天三次”,则会出现 3 个字段来选择时间。

当我运行我的应用程序时,出现以下错误:

Column's children must not contain any null values, but a null value was found at index 13

我发现错误是由这个函数引起的:

  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;
  }

我怎么解决这个问题?

标签: flutterdart

解决方案


您可以替换所有出现的 dateFormat ,例如:

text: DateFormat('hh:mm').format(updatedTime2),

经过

text: updatedTime2 != null ? DateFormat('hh:mm').format(updatedTime2) : "",

您不能为文本赋予空值。

这将允许您的小部件工作,直到用户选择时间。


推荐阅读