提问者:小点点

参数类型'Function'不能赋值给参数类型'void Function()?'. - 'Function'来自'dart: core'


这是我的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,

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

这是我的问题. 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个答案

匿名用户

onPressed: () => selectHandler,

匿名用户

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

final Function selectHandler;

final Function() selectedHandler