【问题标题】:Column's children must not contain any null values, but a null value was found at index 13. Flutter Function列的子项不得包含任何空值,但在索引 13 处找到空值。 Flutter 函数
【发布时间】:2021-06-10 04:18:03
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以替换所有出现的日期格式,例如:

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

    通过

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

    你不能给文本赋空值。

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

    【讨论】:

      【解决方案2】:

      那是因为当所有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;
        }
      

      【讨论】:

      • 像@antonin-gavrel 一样,您需要检查代码中的每个updatedTime。请尝试通过注释所有else if 部分来测试您的代码,然后逐渐取消注释else if 部分直到您发现问题为止。
      猜你喜欢
      • 1970-01-01
      • 2020-12-08
      • 2020-12-02
      • 2021-10-29
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多