Flutter:showDialog:构建函数返回null

时间:2018-11-27 11:16:35

标签: dart flutter

我有一个StatefulWidget。然后,当我单击一个按钮时,它将显示一个警报对话框。当我实施时:

onTap: () {
    showDialog(
        context: context,
        builder: (BuildContext context) {
            return AlertDialog(
                  title: Text("Hello"),
            ); 
        }

}

一切正常。但是,当我将构建器中的内容转移到另一个StatefulWidget时,就会发生此错误:

A build function returned null.
I/flutter ( 3647): The offending widget is: Builder
I/flutter ( 3647): Build functions must never return null. To return an empty space that causes the building widget to
I/flutter ( 3647): fill available room, return "new Container()". To return an empty space that takes as little room as
I/flutter ( 3647): possible, return "new Container(width: 0.0, height: 0.0)".

代码如下:

Here is the calling StatefulWidget:
onTap: () {

            showDialog(
              context: context,
              builder: (BuildContext context) {
                 LastVacDialog(
                  currentDose: currDose,
                  currentDate: currDate,
                  currentIndex: i,
                  setValue: changeDoseValueAndDate,
                ); 

              },
            );
          },

Here is the new StatefulWidget:
class LastVacDialog extends StatefulWidget {
    LastVacDialog({
    this.currentDose,
    this.currentDate,
    this.setValue,
    this.currentIndex,
  });

  final int currentDose;
  final DateTime currentDate;
  final void Function(int, DateTime, int) setValue;
  final currentIndex;

  @override
  LastVacDialogState createState() => new LastVacDialogState();
}

class LastVacDialogState extends State<LastVacDialog> {
    int _dose;
    DateTime _today;




   @override
   Widget build(BuildContext context) {
       return AlertDialog(
           title: Text("Last Dose"),
       );
    }
}

我的代码有问题吗?为了简单起见,我只是省略了一些变量。

1 个答案:

答案 0 :(得分:4)

在-Return前面添加单词LastVacDialog

builder: (BuildContext context) {
              return LastVacDialog(
                     ...

由于错误正在陈述Build函数一定不能返回null。因此,通过在其前面添加return来返回LastVacDialog窗口小部件。