繁体   English   中英

列的子项不得包含任何 null 值,但在索引 13 处找到 null 值。 Flutter Z866408593C2F8BDF27

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM