73

我想实现制作一个带有不同项目的抽屉,所以我DrawerItems使用构造函数为和创建一个单独的文件,将数据传递给主文件。但我在函数上收到以下错误onPressed

"The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
class DrawerItem extends StatelessWidget {
    
      final String text;
      final Function onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }

有谁知道为什么?

4

11 回答 11

196

Change your code to accept a VoidCallback instead of Function for the onPressed.
By the way VoidCallback is just shorthand for void Function() so you could also define it as final void Function() onPressed;

Updated code:

class DrawerItem extends StatelessWidget {
    
      final String text;
      final VoidCallback onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }
于 2020-10-24T16:30:25.713 回答
72

Dart 2.12(零安全):

代替

final Function? onPressed; // Bad

利用

final void Function()? onPressed; // Good
final VoidCallback? onPressed; // Good
于 2021-05-29T08:20:30.763 回答
5

那是因为onPressedinsideFlatButton不是一个正常的函数它的VoidCallBack函数。你可以尝试这样的事情:

final VoidCallBack onPressed;

同时,您正在将法线传递functionVoidCallBack

在此处关注官方文档

在此处输入图像描述

更新代码:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  _myFunction() => print("Being pressed!");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            DrawerItem(
              text: "Hello Jee",
              onPressed: _myFunction,
            ),
          ],
        ),
      ),
    );
  }
}

class DrawerItem extends StatelessWidget {
  final String text;
  final Function onPressed;

  const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text(
        text,
        style: TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 18.0,
        ),
      ),
      onPressed: onPressed,
    );
  }
}
于 2020-10-22T14:21:18.060 回答
5

定义函数时,将括号放在()关键字后面。Function

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  final Function() onPressed; //parenthesis () on function
  final String name;
  const Answer(this.name, this.functionMessage, {Key? key}) : super(key: 
key);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: const EdgeInsets.only(top: 10),
        width: double.infinity,
        child: ElevatedButton(
           style: ElevatedButton.styleFrom(
           primary: Colors.black, // background
           onPrimary: Colors.white, // foreground
          ),
          onPressed: onPressed,
          child: Text(name),
        ));
  }
}
于 2021-10-05T10:13:40.740 回答
3

利用:

VoidCallback? _onPressed

代替:

VoidCallback _onPressed

这对我有用!

于 2021-07-02T04:22:33.207 回答
3

如果您仍然收到此错误“参数onPressed不能有值,null因为它的类型,但隐含的默认值是null。” 修改onPressed功能后,只需应用空安全来解决这样的问题;

final VoidCallback? onPressed;

这适用于Dart 2.x

于 2021-05-01T07:28:25.057 回答
0

利用:

dynamic onPressed;

或者

onPressed;它将自动检测或将参数类型设置为动态

代替:

VoidCallback onPressed;

于 2022-01-15T17:30:42.163 回答
0

2021:如果您有一个接受多个参数的构造函数,您可能希望使用命名参数以避免混淆:

示例:应答按钮小部件

class AnswerButton extends StatelessWidget {
    
      late final Function()? submitAnswer;
    
      Answer({injectMethod}) {
        this.selectHandler = submitAnswer;
      }
    }

主要的

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text("App Title"),
      ),
      body: Column(
        children: [
          AnswerButton(injectMethod: _nextQuestion) /* <--- named argument */
        ],
      ),
    ));
  }
于 2021-07-28T17:01:50.190 回答
0

如果检查了以上所有解决方案,请参考以下代码

class ManageMenu extends StatelessWidget {
  const ManageMenu(
      {Key? key,
      required this.faIcon,
      required this.text,
      required this.onTapFunction})
      : super(key: key);

  final FaIcon faIcon;
  final String text;
  final Function onTapFunction;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => onTapFunction,
      child: ListTile(
        leading: faIcon,
        title: Text(text),
      ),
    );
  }
}

你可能写了错误的语法 asonTap:onTapFunction而不是onTap: () => onTapFunction

于 2021-11-27T18:47:38.847 回答
0

您可以使用 void Function 而不是 Function 并且可以像这样指定函数的类型:

void Function(bool) onPressed;

 

于 2021-09-13T23:38:22.830 回答
-1

如果您在调用时发送参数,您 DrawerItem 还应该添加一个参数Function final void Function(new par) onPressed;

于 2021-07-27T13:51:15.043 回答