Я хочу реализовать диалог именно , как показано в примере в документации go_router :
final GoRouter _router = GoRouter(
routes: <GoRoute>[
GoRoute(
path: '/',
onExit: (BuildContext context) => showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Do you want to exit this page?'),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Go Back'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Confirm'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
},
),
),
],
);
Когда я копирую и вставляю пример кода, в vscode отображается следующая ошибка:
The return type 'Future<bool?>' isn't a 'FutureOr<bool>', as required by the closure's context.dart return_of_invalid_type_from_closure
. Я предполагаю, что этот пример еще не обновлен для обеспечения нулевой безопасности.
К сожалению, у меня еще недостаточно опыта работы с Flutter, чтобы сделать этот пример нулевым.
Есть идеи, как заставить этот пример работать?
Спасибо за вашу помощь!
попробуйте дождаться ответа showDialog:
var response = await showDialog<bool>(...
и тогда вы сможете вернуть ответ
return response ?? false;
полный код:
onExit: (BuildContext context) async {
var response = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Do you want to exit this page?'),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Go Back'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Confirm'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
},
);
return response ?? false;
},