简体   繁体   中英

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

I develop an app that reminds elderly about their medication time. here they have to select the dose of their medication, such as once a day and twice a day. and I create a function that takes this dose and return fields equals to these times. such as, if user select 'three times a day', 3 fields will appear to select time.

When I run my app, there is an error appears which is:

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

and I found that error is caused by this 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;
  }

How can I solve this problem?

You can replace all occurences of your dateFormat like:

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

by

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

You cannot give null value to text.

This will allow your widget to work until user select a time.

That's because you don't have a default value when all the if checking is not matching.

You need to add the else part for that. Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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