不能将参数类型“Function”分配给参数类型“void Function()?”。 -“功能”来自“dart:core”

时间:2021-06-28 09:28:51

标签: flutter dart flutter-layout flutter-dependencies

这是我的 main.dart 文件

import 'package:flutter/material.dart';
import './question.dart';
import 'Answer.dart';

void main() {
 runApp(Appone());
}

class Appone extends StatefulWidget {
 @override
 ApponeState createState() => ApponeState();
}

class ApponeState extends State<Appone> {
 var questionnumber = 0;

 var questions = ["q1", "q2", "q3", "q4"];

 void clikk() {
   print("Darkness");
 setState(() {
  if (questionnumber < questions.length) {
    questionnumber = questionnumber + 1;
    //answer();
  }
});
print(questionnumber);
}

 // void previous() {
 //   if (questionnumber > 0) {
 //     questionnumber = questionnumber - 1;
 //     print("qustionnumber:");
 //     print(questionnumber);
 //   }
 // }

 @override
 Widget build(BuildContext context) {
  return MaterialApp(
   home: Scaffold(
      appBar: AppBar(
        title: Text("First app"),
      ),
      body: Column(
        children: [
          question(questions[questionnumber]),

          Answer(clikk),
          Answer(clikk),

          // ignore: deprecated_member_use

          RaisedButton(
            onPressed: clikk,
            child: Text("meoww1"),
          ),

          // ElevatedButton(
          //   child: Text("ElevatedButton"),
          //   onPressed: () => print("it's pressed"),
          //   style: ElevatedButton.styleFrom(
          //     primary: Colors.green,
          //     onPrimary: Colors.white,
          //     shape: RoundedRectangleBorder(
          //       borderRadius: BorderRadius.circular(32.0),
          //     ),
          //   ),
          // ),

          // RaisedButton(
          //   onPressed: null,
          //   child: Text("meoww2"),
          // ),
          // RaisedButton(
          //   onPressed: () {
          //     print("ans 3");
          //   },
          //   child: Text("meoww3"),
          // ),
          // RaisedButton(
          //   onPressed: null,
          //   child: Text("prev"),
          // ),
          ],
        )),
    );
 }
}

这是我的 Answer.dart 文件

`import 'package:flutter/material.dart';

// ignore: camel_case_types
class Answer extends StatelessWidget {
final Function selectHandler;
Answer(this.selectHandler);
// void nng() {
//   print("nishant");
//   ApponeState a = new ApponeState();
//   a.clikk();

// }

@override
Widget build(BuildContext context) {
return Container(
  width: double.infinity,
  child: ElevatedButton(
    //color: Colors.blueAccent,
    //onPressed: nishant,
    onPressed: selectHandler,

这是我在 onPressed 字段中遇到错误的地方。 selecthandler 是一个函数,它说该函数不能分配给类型 void 函数。 孩子:文本(“喵”), ), ); } }

这是我的 question.dart 文件

import 'package:flutter/material.dart';

class question extends StatelessWidget {
var questionText;
question(this.questionText);

@override
Widget build(BuildContext context) {
 return Container(
    margin: EdgeInsets.all(10),
    decoration: BoxDecoration(
        color: Colors.yellow[100],
        border: Border.all(
          color: Colors.red,
          width: 5,
        )),
    width: double.infinity,
    child: Text(
      questionText,
      style: TextStyle(fontSize: 50, color: Colors.greenAccent),
      textAlign: TextAlign.center,
    ));
   }
  }

你能帮我做点什么吗。

2 个答案:

答案 0 :(得分:0)

this.$toasts.showToast({
  color: 'red',
  type: 'error',
  message: err.message,
})

答案 1 :(得分:0)

在 Answer.dart 中,您的 selectHandlerFunction 类型而不是 Function() 类型,并且您的 clickk 是 Function( )。 只需在您的 Answer.dart 中更改此内容

final Function selectHandler;

final Function() selectedHandler