嗨,我是新来的Flutter和使用BlueStacks作为一个AVD。我看到这个错误在BlueStacks。VS代码不显示任何错误。enter image description here
运行应用后,我在Debug Consle中看到这些消息:enter image description hereenter image description here
下面是我的main.dart代码:
import 'package:flutter/material.dart';
import './quiz.dart';
import './result.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final _questions = const [
{
'questionText': 'whats ur favorite animal',
'answers': [
{'Text': 'khar', 'score': '10'},
{'Text': 'gaav', 'score': '7'},
{'Text': 'asb', 'score': '1'},
{'Text': 'boz', 'score': '3'},
],
},
{
'questionText': 'whats ur favorite color',
'answers': [
{'Text': 'siah', 'score': '10'},
{'Text': 'sabz', 'score': '3'},
{'Text': 'sefid', 'score': '1'},
{'Text': 'sorkh', 'score': '5'},
],
},
{
'questionText': 'whats ur favorite name',
'answers': [
{'Text': 'reza', 'score': '1'},
{'Text': 'reza', 'score': '1'},
{'Text': 'reza', 'score': '1'},
{'Text': 'reza', 'score': '1'}
],
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _resetQuiz() {
setState(() {
_questionIndex = 0;
_totalScore = 0;
});
}
void _answerQuestion(int score) {
_totalScore = _totalScore + score;
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
if (_questionIndex < _questions.length) {
print('we have more question!');
} else {
print('no more question!');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('my first app'),
),
body: _questionIndex < _questions.length
? Quiz(
answerQuestion: _answerQuestion,
questionIndex: _questionIndex,
questions: _questions,
)
: Result(_totalScore, _resetQuiz),
),
);
}
}
字符串
下面是我的quiz.dart文件:
import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
class Quiz extends StatelessWidget {
final List<Map<String, Object>> questions;
final int questionIndex;
final Function answerQuestion;
Quiz(
{required this.questions,
required this.answerQuestion,
required this.questionIndex});
@override
Widget build(BuildContext context) {
return Column(
children: [
Question(
questions[questionIndex]['questionText'] as String,
),
...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
.map((answer) {
return Answer(
() => answerQuestion(answer['score']), answer['text'] as String);
}).toList()
],
);
}
}
型
answer.dart文件:
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
final String answerText;
Answer(this.selectHandler, this.answerText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
onPressed: selectHandler,
child: Text(answerText),
),
);
}
}
型
和result.dart文件:
import 'package:flutter/material.dart';
class Result extends StatelessWidget {
final int resultScore;
final VoidCallback resetHandler;
Result(this.resultScore, this.resetHandler);
String get resultPhrase {
String resultText;
if (resultScore <= 8) {
resultText = 'You are awsome and Bigonah';
} else if (resultScore <= 12) {
resultText = 'Bad ni';
} else if (resultScore <= 16) {
resultText = 'You are Strange';
} else {
resultText = 'Go Doctor!';
}
return resultText;
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Text(
resultPhrase,
style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
onPressed: resetHandler,
child: Text('Restart Quiz!'),
),
],
),
);
}
}
型
1条答案
按热度按时间kd3sttzy1#
该错误告诉您以下行有问题:
字符串
这里的错误是,你从map中阅读了一个名为
text
的值,但map中的键的名称是Text
:型