flutter - 如何在颤动中访问地图元素并将它们显示为按钮?
问题描述
我在尝试学习 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()
],
),
),
);
}
}
解决方案
ListView.builder(
shrinkWrap:true,
itemCount:questions.length,
itemBuilder:(c,i){
return ElevatedButton(
onPressed:(){
//todo handle click event
},
child: Text(questions[i]['questionText']),
);
}
)
推荐阅读
- haskell - 后卫可以有多个结果吗?
- angular - 如何强制 ngrok 将请求发送回 localhost
- android - Jetpack Compose 如何将特定大小的文本组件的文本或内容与左下角或右下角对齐?
- ios - 我是新手,但我在 pod 安装时需要帮助,我的 pod 文件也附上了
- android - 如何查找我是否有后台定位服务?我只使用 ACCESS_FINE_LOCATION 前台
- c - 预加载库函数调用者的行号
- php - 如何在php中将指数变量转换为完整的十进制变量?
- common-lisp - sdl2:load-bmp 当前工作目录的问题,common-lisp
- java - 程序中使用的事件监听器导致应用程序崩溃请告诉我使用 eventListener 的正确方法是什么
- node.js - 迁移:使用 TypeORM 的 queryrunner 代替 sql 语句