【发布时间】:2021-09-13 07:55:37
【问题描述】:
我在尝试学习 Flutter 课程时遇到错误。
有效的是它显示问题。 我正在努力寻找答案。它应该从我创建的地图中选择答案。
错误说: 元素类型“Iterable”不能分配给列表类型“Widget”。
这是我的 main.dart:
import 'package:flutter/material.dart';
import 'package:quizapp/answer.dart';
import 'package:quizapp/question.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
var _questionIndex = 0;
// increases the state of questionIndex by 1
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
}
var questions = [
{
'questionText': 'Question 1',
'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
},
{
'questionText': 'Question 2',
'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
},
];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Meine erste App'),
),
body: Column(
children: <Widget>[
Question(
questions[_questionIndex]['questionText'].toString(),
),
questions.map((question) {
return Answer(question);
})
],
),
),
);
}
}
这是我的回答。飞镖:
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final void Function() selectHandler;
Answer(this.selectHandler);
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width / 1.2,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red,
onPrimary: Colors.white,
),
child: Text('Antwort 1'),
onPressed: selectHandler,
),
);
}
}
感谢您的帮助
所以我更加关注课程。现在在 Flutter 应用程序中出现了错误: “String”类型不是类型转换中“List”类型的子类型
这是我在 main.dart 中的代码
import 'package:flutter/material.dart';
import 'package:notizapp/answer.dart';
import 'package:notizapp/question.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
var _questionIndex = 0;
// increases the state of questionIndex by 1
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
}
var questions = [
{
'questionText': 'Question 1',
'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
},
{
'questionText': 'Question 2',
'answers': ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4']
},
];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Meine erste App'),
),
body: Column(
children: <Widget>[
Question(
questions[_questionIndex]['questionText'].toString(),
),
...(questions[_questionIndex]['questionText'] as List<String>)
.map((answer) {
return Answer(_answerQuestion, answer);
}).toList()
],
),
),
);
}
}
【问题讨论】: