This function has a 'Widget' return type, but does not end with a return statement

Asked

Viewed 20 times

0

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  DataBaseHelper db = DataBaseHelper();
  @override
  Widget build(BuildContext context) {
    final Map<String, dynamic> args = ModalRoute.of(context).settings.arguments;
    Session session = args['session'];
    void selecao(escolha) async {
      if (escolha == 'logout') {
        session.autenticado = 0;
        await db.updateSession(session.userId, session);
        Navigator.popAndPushNamed(context, '/');
      }
    }

    return WillPopScope(
      onWillPop: _onBackPressed,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Home'),
          automaticallyImplyLeading: false,
          actions: [
            PopupMenuButton(
              onSelected: selecao,
              itemBuilder: (context) {
                return [
                  PopupMenuItem(
                    child: Text('Sair'),
                    value: 'logout',
                  )
                ];
              },
            )
          ],
        ),
        body: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            RaisedButton(
              padding: const EdgeInsets.all(8.0),
              textColor: Colors.black,
              color: Colors.yellow,
              child: Text('Contatos'),
              onPressed: () {
                Navigator.pushNamed(context, '/home/contatos');
              },
            ),
            RaisedButton(
              padding: const EdgeInsets.all(8.0),
              textColor: Colors.black,
              color: Colors.yellow,
              child: Text('Mapas'),
              onPressed: () {
                Navigator.pushNamed(context, '/home/mapa');
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<bool> _onBackPressed() {
    return showDialog(
        context: context,
        builder: (context) {
          AlertDialog(
            title: Text('Deseja sair?'),
            actions: [
              FlatButton(
                child: Text('Sim'),
                onPressed: () {
                  SystemChannels.platform.invokeMethod('SystemNavigator.pop');
                },
              ),
              FlatButton(
                child: Text('Não'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        }
      );
  }
}
  • How can I solve? Follow the code (I think I can understand).

1 answer

1


I believe that error itself makes explicit what is wrong. And it should even be indicating which line is the problem, but you did not put stacktrace in the question..

So based on the code provided, the problem must be in the showDialog, you’re not returning anything in it.

Notice in the signing of Typedef from the Builder he receives:

Widget WidgetBuilder (BuildContext context)

It is mandatory to return a Widget.

Future<bool> _onBackPressed() {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog...
        }
      );
  }

Add the return in the AlertDialog.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.