Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: showDialog: build function returned null

Tags:

flutter

dart

I have a StatefulWidget. Then when I click a button, it shows an alert dialog. When I implement:

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

}

Everything works fine. But when I transfered the things inside the builder to a different StatefulWidget, then this error occurs:

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 code:

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"),
       );
    }
}

Is there something wrong with my code? I just omitted some variables for simplicity.

like image 329
nypogi Avatar asked Nov 27 '18 11:11

nypogi


1 Answers

Add the Word Return in Front of - LastVacDialog

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

As Error is Stating Build function must never return null. So return your LastVacDialog Widget by adding return in front of it.

like image 116
anmol.majhail Avatar answered Nov 10 '22 02:11

anmol.majhail