参数类型'Future<dynamic> Function()'不能分配给参数类型'Future<bool> Function()'?

0 人关注

我是Flutter的新手,我无法解决这个错误,如果有人能帮助我解决这个问题,我将非常感激。

我声明这个函数,编译器显示错误在函数声明中。

Future _onWillPop() {
    //return _Pop(); //???
    return 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>[
            TextButton(
                onPressed: () => Navigator.of(context).pop(false),
                child: new Text('No')),
            TextButton(
                onPressed: () {
                  disconnectFromDevice();
                  Navigator.of(context).pop(true);
                child: new Text('Yes')),

这是我声明函数的地方,也是错误出现的地方。

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,                //Declaration highlighted as error
      child: Scaffold(
        appBar: AppBar(
          title: Text('Sensor'),
        body: Container(
            child: !isReady
                ? Center(
              child: Text(
                "Waiting...",
                style: TextStyle(fontSize: 24, color: Colors.red),
                : Container(
              child: StreamBuilder<List<int>>(
                stream: stream,
                builder: (BuildContext context,
                    AsyncSnapshot<List<int>> snapshot) {
                  if (snapshot.hasError)
                    return Text('Error: ${snapshot.error}');
                  if (snapshot.connectionState ==
                      ConnectionState.active) {
                    var currentValue = _dataParser(snapshot.data!);
                    return Center(
                        child: Stack(
                          children: <Widget>[
                            Container(
                              child: Center(
                                child: new Object3D(
                                  size: const Size(300, 300),
                                  path: "assets/3d.obj",
                                  asset: true,
                                  angleX: currentValue.x,
                                  angleY: currentValue.y,
                                  angleZ: currentValue.z,
                                  zoom: 50,
                              color: Colors.blueGrey,
                            Column(children: <Widget>[
                              Text('Current value from Sensor',
                                  style: TextStyle(fontSize: 14)),
                              Text('${currentValue.x}',
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16)),
                              Text('${currentValue.y}',
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16)),
                              Text('${currentValue.z}',
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16))
                  } else {
                    return Text('Check the stream');

错误。不能以健全的null safety运行,因为以下依赖项 不支持无效安全。

  • package:flutter_3d_obj
  • For solutions, see https://dart.dev/go/unsound-null-safety lib/mpu6050_page.dart:129:18: 错误。参数类型'Future Function()'不能被分配给参数类型'Future Function()?'。

  • 'Future' is from 'dart:async'. onWillPop: _onWillPop,

    失败。构建失败,出现异常。

    脚本 'C:\Users\adria\flutter\packages\flutter_tools\gradle\flutter.gradle' 行: 1156

  • 出了什么问题。 任务':app:compileFlutterBuildDebug'的执行失败。

  •