Return 类型无效但不知道为什么? Flutter/Dart
Return of invalid type but don't know why? Flutter/Dart
嘿,我是 flutter 的新手,最近我一直在开发一个移动应用程序,它通过 BLE 从 ESP32 接收数据,但我遇到了一个问题,如果我想让用户像这样断开与设备的连接:
Future<bool> _onWillPop() {
return showDialog(
context: context,
builder: (context) =>
new AlertDialog(
title: Text('Are you sure?'),
content: Text('Do you want to disconnect device
and go back?'),
actions: <Widget>[
new ElevatedButton(
onPressed: () =>
Navigator.of(context).pop(false),
child: new Text('No')),
new ElevatedButton(
onPressed: () {
disconnectFromDevice();
Navigator.of(context).pop(true);
},
child: new Text('Yes')),
],
) ??
false);
}
它给我错误警告:
A value of type 'Future<dynamic>' can't be returned from the method '_onWillPop' because it has a return type of 'Future<bool>'.
The return type 'Object' isn't a 'Widget', as required by the closure's context.
但以我目前的知识,我不知道如何解决我的问题。如果有人能帮助我,我将非常感激 :) 对于任何语法错误,我深表歉意
Future<T?> showDialog
Future<T?> showDialog<T>(
{required BuildContext context,
required WidgetBuilder builder,
bool barrierDismissible = true,
不需要 return
错误说 showDialog
的 return
类型不是 bool。相反,您可以只替换为 await 然后 return bool.
下面是您可以提供的代码。
Future<bool> _onWillPop(BuildContext context) async {
bool shouldPop = false;
await showDialog(
context: context,
builder: (context) =>
AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to disconnect device and go back?'),
actions: <Widget>[
ElevatedButton(
onPressed: () {
// shouldPop is already false
},
child: const Text('No')),
ElevatedButton(
onPressed: () async {
await disconnectFromDevice();
Navigator.of(context).pop();
shouldPop = true;
},
child: const Text('Yes')),
],
));
return shouldPop;
}
我稍微更改了代码,如果您不想弹出,则 return 为假,如果您想弹出,则 return 为真。您可以根据需要更改代码。
嘿,我是 flutter 的新手,最近我一直在开发一个移动应用程序,它通过 BLE 从 ESP32 接收数据,但我遇到了一个问题,如果我想让用户像这样断开与设备的连接:
Future<bool> _onWillPop() {
return showDialog(
context: context,
builder: (context) =>
new AlertDialog(
title: Text('Are you sure?'),
content: Text('Do you want to disconnect device
and go back?'),
actions: <Widget>[
new ElevatedButton(
onPressed: () =>
Navigator.of(context).pop(false),
child: new Text('No')),
new ElevatedButton(
onPressed: () {
disconnectFromDevice();
Navigator.of(context).pop(true);
},
child: new Text('Yes')),
],
) ??
false);
}
它给我错误警告:
A value of type 'Future<dynamic>' can't be returned from the method '_onWillPop' because it has a return type of 'Future<bool>'.
The return type 'Object' isn't a 'Widget', as required by the closure's context.
但以我目前的知识,我不知道如何解决我的问题。如果有人能帮助我,我将非常感激 :) 对于任何语法错误,我深表歉意
Future<T?> showDialog
Future<T?> showDialog<T>(
{required BuildContext context,
required WidgetBuilder builder,
bool barrierDismissible = true,
不需要 return
错误说 showDialog
的 return
类型不是 bool。相反,您可以只替换为 await 然后 return bool.
下面是您可以提供的代码。
Future<bool> _onWillPop(BuildContext context) async {
bool shouldPop = false;
await showDialog(
context: context,
builder: (context) =>
AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to disconnect device and go back?'),
actions: <Widget>[
ElevatedButton(
onPressed: () {
// shouldPop is already false
},
child: const Text('No')),
ElevatedButton(
onPressed: () async {
await disconnectFromDevice();
Navigator.of(context).pop();
shouldPop = true;
},
child: const Text('Yes')),
],
));
return shouldPop;
}
我稍微更改了代码,如果您不想弹出,则 return 为假,如果您想弹出,则 return 为真。您可以根据需要更改代码。