import 'package:first_app/answer.dart';
import 'package:first_app/question.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _indexQuestion = 0;
void _answerQuestion() {
setState(() {
_indexQuestion = _indexQuestion 1;
});
}
@override
Widget build(BuildContext context) {
var questions = [
{
"questionText": "Whats your favorite color?",
"answer": ["Blue", "Yellow", "White", "Grey"]
},
{
"questionText": "Whats your favorite animal?",
"answer": ["Snake", "Rabbit", "Lion", "Gazele"]
},
{
"questionText": "Who's your favorite instructor?",
"answer": ["Max", "Felix", "Ronaldo", "Suarez"]
},
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Quiz App"),
backgroundColor: Colors.blueGrey,
),
body: Column(
children: [
Questions(questions[_indexQuestion]["answer"] as String),
...(questions[_indexQuestion]["answer"] as List<String>)
.map((answers) {
return Answer(_answerQuestion, answers);
}).toList()
],
),
),
);
}
}
伙計們正在嘗試將字串串列轉換為 flutter 中的小部件,在代碼本身中它沒有顯示任何錯誤,但是模擬器說這個 -> 型別“串列”不是型別轉換中“字串”型別的子型別。我做錯了什么?
我還加了個圖,大家可以看看。
uj5u.com熱心網友回復:
更改Questions(questions[_indexQuestion]["answer"] as String),為Questions(questions[_indexQuestion]["questionText"] as String),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/401632.html
