so I encounter this problem. So I have this function
int _questionIndex = 0;
int _totalScore = 0;
void _answerQuestion(int score) {
setState(() {
_questionIndex = _questionIndex 1;
_totalScore = score;
});
}
I plan to pass this to 2 widgets, quiz and answers
quiz
import 'package:app_1_starter/answer.dart';
import 'package:app_1_starter/question.dart';
import 'package:flutter/material.dart';
class Quiz extends StatelessWidget {
final int index;
final List<Map<String, Object>> questions;
final Function pressHandler;
const Quiz(
{Key? key,
required this.questions,
required this.index,
required this.pressHandler})
: super(key: key);
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: <Widget>[
Question(questions[index]['question'] as String),
...(questions[index]['answers'] as List<Map<String, Object>>)
.map((answer) {
return Answer(
() => pressHandler(answer['score']),
answer['text'] as String,
);
}).toList(),
],
),
);
}
}
the quiz is for when there's only some questions to be answered left.
and this is the answer widget which will provide the answer layout
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final String buttonText;
final Function pressHandler;
const Answer(this.pressHandler, this.buttonText, {Key? key})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: const EdgeInsets.all(5),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: pressHandler(),
child: Text(
buttonText,
style: const TextStyle(color: Colors.white),
),
),
);
}
}
my sdk is environment: sdk: ">=2.17.6 <3.0.0"
the error keeps saying
The following NoSuchMethodError was thrown building Answer(dirty):
Closure call with mismatched arguments: function '_MyAppState.build.<anonymous closure>'
Receiver: Closure: () => ({int score}) => void
Tried calling: _MyAppState.build.<anonymous closure>(30)
Found: _MyAppState.build.<anonymous closure>() => ({int score}) => void
I've tried to put the answer to quiz yet, it keeps giving the same error like above.
I don't know what that means and what went wrong here. Thanks for coming
cheers
CodePudding user response:
thanks to @mmcdon20 for your suggestion the problem is on the main.dart.
It should be like this
Quiz(
questions: _questions,
index: _questionIndex,
pressHandler: _answerQuestion,
)