类型“Null”不是类型“Function”的子类型

Zab*_*lah 5 android dart android-studio flutter

我是颤振新手。我正在构建一个测验应用程序,并拥有以下三个 dart 文件:

\n

主程序.dart

\n
import 'package:flutter/material.dart';\nimport './answer.dart';\nimport './question.dart';\n\nvoid main(){\n  runApp(MyApp());\n}\n\nclass MyApp extends StatefulWidget {\n  State<StatefulWidget> createState(){\n    return _MyAppState();\n  }\n}\n\nclass _MyAppState extends State<MyApp>{\n  var _questionIndex = 0;\n  _answerQuestion(){\n    setState(() {\n      _questionIndex = _questionIndex + 1;\n    });\n}\n  @override\n  Widget build(BuildContext context) {\n    var questions = [\n      {'questionText': 'What\\'s your favourite color ?',\n        'answers': ['Red','Blue','White','Black']\n      },\n      {'questionText': 'What\\'s your favourite Animal ?',\n        'answers': ['Dog','Rabbit','Tiger','Monkey']\n      },\n      {'questionText': 'What\\'s your favourite Day ?',\n        'answers': ['Tuesday','Monday','Sunday','Friday','Wednesday','Saturday']\n      },\n    ];\n    return MaterialApp(\n      home: Scaffold(\n        appBar: AppBar(\n          title: Text('My First App'),\n        ),\n        body: Column(\n          children: [\n            Question(questions[_questionIndex]['questionText'] as String,\n            ),\n            ...(questions[_questionIndex]['answers'] as List).map((answer) {\n              return Answer(_answerQuestion(),answer);\n            }).toList()\n    ],\n        )\n      ),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

问题.dart

\n
import 'package:flutter/material.dart';\n\n\nclass Question extends StatelessWidget {\n  final String questions;\n  Question(this.questions);\n\n  @override\n  Widget build(BuildContext context) {\n    return Container(\n      width: double.infinity,\n      margin: EdgeInsets.all(10),\n      child:(\n      Text(\n      questions,\n      style: TextStyle(\n          fontSize: 25),\n      textAlign: TextAlign.center,)\n    ),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

答案.dart

\n
import 'package:flutter/material.dart';\n\nclass Answer  extends StatelessWidget {\n  final Function buttonHandler;\n  final String answer;\n\n  Answer(this.buttonHandler,this.answer);\n\n  @override\n  Widget build(BuildContext context) {\n    return Container(\n      width: double.infinity,\n      child: ElevatedButton(\n        child: Text(answer),\n        style: ButtonStyle(\n            backgroundColor: MaterialStateProperty.all(Colors.blue),\n            foregroundColor: MaterialStateProperty.all(Colors.white)\n        ),\n        onPressed: () => buttonHandler,\n    ),\n    );\n  }\n}\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

当我在 Android Studio 中的 Android 上运行该应用程序时,出现以下错误:

\n
\n
\n

\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 小部件库捕获异常\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\ x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\ x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\ xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\ x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\ x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90
\n构建 MyApp 时抛出以下 _TypeError(脏,状态: _MyAppState#7f7de):
\n类型“Null”不是“Function”类型的子类型
\n导致错误的相关小部件是:
\nMyApp file:///C:/src/first_app/lib/main.dart:7: 10

\n
\n
\n

nvo*_*igt 6

这:

onPressed: () => buttonHandler,
Run Code Online (Sandbox Code Playgroud)

需要是:

onPressed: buttonHandler,
Run Code Online (Sandbox Code Playgroud)

或者

onPressed: () => buttonHandler(),
Run Code Online (Sandbox Code Playgroud)

取决于您的处理程序是否与所需的签名完全匹配。

另外,这个:

return Answer(_answerQuestion(),answer);
Run Code Online (Sandbox Code Playgroud)

需要是

return Answer(_answerQuestion,answer);
Run Code Online (Sandbox Code Playgroud)

一般来说,您已经多次混淆了调用方法和将方法作为参数传递,您可能想更熟悉它。