首先,我真的很新
我不明白为什么会出现这两个错误: 1.元素类型“String”不能分配给列表类型“Widget”。 2.预计会找到']'。
import 'package:flutter/material.dart';
import './qs.dart';
import 'answer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
//override is basically useless
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
} //refers to the particular class
}
class _MyAppState extends State<MyApp> {
//an underscore means that the particular class can only be called
//within htat particular file
var _questionindex = 0;
@override
void _answerQuestion() {
setState(() {
_questionindex = _questionindex + 1;
});
print(_questionindex);
} //helps in reloading the state whenever the values of a vriable is updated
@override
Widget build(BuildContext context) {
var questions = [
{
'questiontext':'What\s your Age group?',
'answers':['No','No','No','No'],
},
{
'questiontext':'What\s your profession?',
'answers':['student','teacher','government official','[private enterprise']
},
{
'questiontext':'At what time do you get up?',
'answers':['4am-6am','6am-7am','7am-8am','After 8am']}
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text(
'Dragons be here',
style: TextStyle(
fontFamily: "Arial",
fontSize: 35,
fontWeight: FontWeight.bold,
),
),
titleSpacing: 1.0,
centerTitle: true,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(bottom: Radius.circular(30)),
),
),
body: Column(
children: <Widget>[
qs(
questions[_questionindex]['questiontext'] as String,
),
...(questions[_questionindex]['answers']) as List<String>.map((answer){
Answer(_answerQuestion,answer);
}).toList()
],
),
),
);
}
}
import 'package:flutter/material.dart';
class qs extends StatelessWidget {
final String questionStr;
qs(this.questionStr);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: Text(
questionStr,
),
);
}
}
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback ansk;
//idk y but function is snot working,
//if it is not wrking use Voidcallback
final String gimme_da_answer;
Answer(this.ansk, this.gimme_da_answer);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
onPressed: ansk,
child: Text(gimme_da_answer),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.amberAccent),
foregroundColor: MaterialStateProperty.all<Color>(Colors.blueAccent),
),
),
);
}
}
另外,我不太明白 dart 中的 map 是什么,如果有人给出一个简短的概述,那就真的很开心了
如果您已经读到这里,谢谢
最佳答案
...(questions[_questionindex]['answers']) as List<String>.map((answer){
Answer(_answerQuestion,answer);
}).toList()
上述代码导致您出现问题的原因有两个。
-
List<String>.map(...
dart 令人困惑,因为它认为您正在调用List<String>
上的某些方法,要解决此问题,请用另一对括号将其括起来
...((questions[_questionindex]['answers']) as List<String>).map((answer){
Answer(_answerQuestion,answer);
}).toList()
- map 方法不返回任何内容,您很可能忘记添加 return 语句,要解决此问题,只需添加 return 语句即可:
...((questions[_questionindex]['answers']) as List<String>).map((answer){
return Answer(_answerQuestion,answer);
}).toList()
关于期望找到“]”。抱歉,但是在我的编辑器中复制并粘贴您的代码后,我没有收到此错误...您介意显示屏幕截图吗?出现波浪线?
最后我将尝试解释什么是 map :
map 只是一组键值对,也就是说,map 中的每个项目都有一些名称和一些值,类似于普通变量。 map 的语法如下所示:
Map<TypeA, TypeB> myMap = {
key1: value1,
key2: value2,
key3: value3,
...
keyN: valueN,
};
其中 typeA 是键的类型,typeB 是值的类型,例如:
Map<int, bool> myMap = {
4: false,
99: true,
1927: false,
777777: false,
7: true,
};
上面,映射的键具有 int 类型,映射的值具有 bool 类型。
当然,你可以使用dynamic
为了拥有不同类型的 key :
Map<String, dynamic> myMap = {
'some string': 1,
'some other string': true,
'something else': ['aa','vv','gg',1],
};
上面的键的类型始终是字符串,但是值的类型发生了变化,如 some string
,类型为int,为some other string
类型为 bool,对于 something else
,类型为List
,该列表有 3 个字符串和 1 个整数。
您的questions
变量实际上是一个 map 列表:
var questions = [
{
'questiontext':'What\s your Age group?',
'answers':['No','No','No','No'],
},
{
'questiontext':'What\s your profession?',
'answers':['student','teacher','government official','[private enterprise']
},
{
'questiontext':'At what time do you get up?',
'answers':['4am-6am','6am-7am','7am-8am','After 8am']}
];
这些是类型:
List<Map<String, dynamic>> questions = [
{
String: String,
String: List<String>
}
]
例如,var x = questions[0]['answers']
, x 将是动态的,因为 map 的值是动态的。
希望这个解释能让一切变得更清楚一点?
关于Flutter:元素类型 'String'无法分配给列表类型 'Widget',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70381343/