首先,我真的很新
我不明白為什么會出現這兩個錯誤: 1.無法將元素型別“字串”分配給串列型別“小部件”。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 中的地圖是什么,如果有人給出一個簡短的概述,那將是非常糟糕的
如果你讀到這里謝謝
uj5u.com熱心網友回復:
...(questions[_questionindex]['answers']) as List<String>.map((answer){
Answer(_answerQuestion,answer);
}).toList()
上述代碼導致您出現問題的原因有兩個。
List<String>.map(...令人困惑的飛鏢,因為它認為您正在呼叫某個方法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<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以獲得不同型別的密鑰:
Map<String, dynamic> myMap = {
'some string': 1,
'some other string': true,
'something else': ['aa','vv','gg',1],
};
上面的鍵的型別總是字串,但是值的型別會發生變化,forsome string的型別是 int,some other string型別是 bool,而 forsomething else的型別是List,那里的串列有 3 個字串和一個 int。
你的questions變數實際上是一個地圖串列:
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 將是動態的,因為您的地圖的值是動態的。
希望這個解釋讓一切都更清楚一點?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383463.html
下一篇:拋出了另一個例外:RenderBox沒有布局:CustomRenderShrinkWrappingViewport#f6727relayoutBoundary=up17NEEDS-PAINTNEEDS
