【发布时间】:2021-12-16 15:11:25
【问题描述】:
首先,我真的很新
我不明白为什么会出现这两个错误: 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 中的地图是什么,如果有人给出简要概述,那将是真正的 halpful
如果你读到这里,谢谢
【问题讨论】:
-
您在哪一行收到此错误?
-
@Diwyansh 第 73 行显示两个错误 ...(questions[_questionindex]['answers']) 作为 List
.map((answer){ Answer(_answerQuestion,answer ); }).toList()