不能将 'Object?' 类型的值分配给类型为 'bool' 的变量



我的home.dart文件中出现此错误

lib/home.dart(115,29): error G44692867: A value of type 'Object?' can't be assigned to a variable of type 'bool'. [E:Prajwalflutter_projectsquiz_app2buildwindowsflutterflutter_assemble.vcxproj]
lib/home.dart:1
lib/home.dart(125,43): error GC2F972A8: The argument type 'Object?' can't be assigned to the parameter type 'bool'. [E:Prajwalflutter_projectsquiz_app2buildwindowsflutterflutter_assemble.vcxproj]
lib/home.dart:1
lib/home.dart(115,21): error GC2F972A8: The argument type 'MaterialColor?' can't be assigned to the parameter type 'Color' because 'MaterialColor?' is nullable and 'Color' isn't. [E:Prajwalflutter_projectsquiz_app2buildwindowsflutterflutter_assemble.vcxproj]

这是我的家庭艺术文件。。

import 'package:quiz_app2/answer.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Icon> _scoreTracker = [];
int _questionIndex = 0;
int _totalScore = 0;
bool answerWasSelected = false;
bool endOfQuiz = false;
bool correctAnswerSelected = false;
void _questionAnswered(bool answerScore) {
setState(() {
// answer was selected
answerWasSelected = true;
// check if answer was correct
if (answerScore) {
_totalScore++;
correctAnswerSelected = true;
}
// adding to the score tracker on top
_scoreTracker.add(
answerScore
? Icon(
Icons.check_circle,
color: Colors.green,
)
: Icon(
Icons.clear,
color: Colors.red,
),
);
//when the quiz ends
if (_questionIndex + 1 == _questions.length) {
endOfQuiz = true;
}
});
}
void _nextQuestion() {
setState(() {
_questionIndex++;
answerWasSelected = false;
correctAnswerSelected = false;
});
// what happens at the end of the quiz
if (_questionIndex >= _questions.length) {
_resetQuiz();
}
}
void _resetQuiz() {
setState(() {
_questionIndex = 0;
_totalScore = 0;
_scoreTracker = [];
endOfQuiz = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
' Quiz App',
style: TextStyle(color: Colors.white),
),
centerTitle: true,
),
body: Center(
child: Column(
children: [
Row(
children: [
if (_scoreTracker.length == 0)
SizedBox(
height: 25.0,
),
if (_scoreTracker.length > 0) ..._scoreTracker
],
),
Container(
width: double.infinity,
height: 130.0,
margin: EdgeInsets.only(bottom: 10.0, left: 30.0, right: 30.0),
padding: EdgeInsets.symmetric(horizontal: 50.0, vertical: 20.0),
decoration: BoxDecoration(
color: Colors.deepOrange,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: Text(
_questions[_questionIndex]['question'] as String,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
...(_questions[_questionIndex]['answers']
as List<Map<String, Object>>)
.map(
(answer) => Answer(
answerText: answer['answerText'] as String,
answerColor: answerWasSelected
? answer['score']
? Colors.green
: Colors.red
: null,
answerTap: () {
// if answer was already selected then nothing happens onTap
if (answerWasSelected) {
return;
}
//answer is being selected
_questionAnswered(answer['score']);
},
),
),
SizedBox(height: 20.0),
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(double.infinity, 40.0),
),
onPressed: () {
if (!answerWasSelected) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Please select an answer before going to the next question'),
));
return;
}
_nextQuestion();
},
child: Text(endOfQuiz ? 'Restart Quiz' : 'Next Question'),
),
Container(
padding: EdgeInsets.all(20.0),
child: Text(
'${_totalScore.toString()}/${_questions.length}',
style: TextStyle(fontSize: 40.0, fontWeight: FontWeight.bold),
),
),
if (answerWasSelected && !endOfQuiz)
Container(
height: 100,
width: double.infinity,
color: correctAnswerSelected ? Colors.green : Colors.red,
child: Center(
child: Text(
correctAnswerSelected
? 'Well done, you got it right!'
: 'Wrong :/',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
if (endOfQuiz)
Container(
height: 100,
width: double.infinity,
color: Colors.black,
child: Center(
child: Text(
_totalScore > 4
? 'Congratulations! Your final score is: $_totalScore'
: 'Your final score is: $_totalScore. Better luck next time!',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: _totalScore > 4 ? Colors.green : Colors.red,
),
),
),
),
],
),
),
);
}
}
final _questions = const [
{
'question': 'How long is New Zealand’s Ninety Mile Beach?',
'answers': [
{'answerText': '88km, so 55 miles long.', 'score': true},
{'answerText': '55km, so 34 miles long.', 'score': false},
{'answerText': '90km, so 56 miles long.', 'score': false},
],
},
{
'question':
'In which month does the German festival of Oktoberfest mostly take place?',
'answers': [
{'answerText': 'January', 'score': false},
{'answerText': 'October', 'score': false},
{'answerText': 'September', 'score': true},
],
},
{
'question': 'Who composed the music for Sonic the Hedgehog 3?',
'answers': [
{'answerText': 'Britney Spears', 'score': false},
{'answerText': 'Timbaland', 'score': false},
{'answerText': 'Michael Jackson', 'score': true},
],
},
{
'question': 'In Georgia (the state), it’s illegal to eat what with a fork?',
'answers': [
{'answerText': 'Hamburgers', 'score': false},
{'answerText': 'Fried chicken', 'score': true},
{'answerText': 'Pizza', 'score': false},
],
},
{
'question':
'Which part of his body did musician Gene Simmons from Kiss insure for one million dollars?',
'answers': [
{'answerText': 'His tongue', 'score': true},
{'answerText': 'His leg', 'score': false},
{'answerText': 'His butt', 'score': false},
],
},
{
'question': 'In which country are Panama hats made?',
'answers': [
{'answerText': 'Ecuador', 'score': true},
{'answerText': 'Panama (duh)', 'score': false},
{'answerText': 'Portugal', 'score': false},
],
},
{
'question': 'From which country do French fries originate?',
'answers': [
{'answerText': 'Belgium', 'score': true},
{'answerText': 'France (duh)', 'score': false},
{'answerText': 'Switzerland', 'score': false},
],
},
{
'question': 'Which sea creature has three hearts?',
'answers': [
{'answerText': 'Great White Sharks', 'score': false},
{'answerText': 'Killer Whales', 'score': false},
{'answerText': 'The Octopus', 'score': true},
],
},
{
'question': 'Which European country eats the most chocolate per capita?',
'answers': [
{'answerText': 'Belgium', 'score': false},
{'answerText': 'The Netherlands', 'score': false},
{'answerText': 'Switzerland', 'score': true},
],
},
];

错误似乎发生在这个表达式中:

answerColor: answerWasSelected
? answer['score']
? Colors.green
: Colors.red
: null,

当您调用answer['score']时,返回的值可能是null,因此您不能将其用作布尔表达式。

如果预期的score属性是布尔值,则可以使用null合并运算符。

answerColor: answerWasSelected
? answer['score'] ?? false // If answer['score'] is null, returns false
? Colors.green
: Colors.red
: null,

相关内容

最新更新