I can't get rid of null safety and whenever I run this code it shows me that you're trying to expect List but you get List<String, Widget>. Actually, I'm new here, so the way I'm asking might not be good..... but I really need to solve this problem...Thankx
This is answer file (answer.dart):
import 'package:flutter/material.dart';
class Answer extends StatelessWidget
{
final VoidCallback selectHandler;
final String answerText;
Answer(this.selectHandler, this.answerText);
@override
Widget build(BuildContext context)
{
return Container(
child: RaisedButton(
child: Text(answerText),
onPressed: selectHandler,
color: Colors.amber,
),
margin: EdgeInsets.all(10),
width: double.infinity,
);
}
}
This is quiz file (quiz.dart):
import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
class Quiz extends StatelessWidget
{
final VoidCallback answerQuestion;
final List<Map<String, Object>> questions;
final int questionIndex;
Quiz({
@required this.answerQuestion,
@required this.questions,
@required this.questionIndex,
});
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Question(questions[questionIndex]["questionText"] as String),
...(questions[questionIndex]["answerText"]["text"] as List<String>)
.map((answer) {
return Answer(answerQuestion, answer);
}).toList(),
],
),
alignment: Alignment.center,
);
}
}
This is for showing result (result.dart):
import 'package:flutter/material.dart';
class Result extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(child: Text("You did it!"));
}
}
This is for questions (question.dart):
import 'package:flutter/material.dart';
class Question extends StatelessWidget {
final String questionText;
Question(this.questionText);
@override
Widget build(BuildContext context) {
return Container(
child: Text(
questionText,
style: TextStyle(
fontSize: 30,
),
textAlign: TextAlign.center,
),
width: double.infinity,
margin: EdgeInsets.all(10),
);
}
}
This is main file (main.dart):
import 'package:flutter/material.dart';
import './quiz.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _totalScore = 0;
var _questionIndex = 0;
//some random questions
var _questions = [
{
"questionText":
"What year was the first Iron Man movie released, kicking off the Marvel Cinematic Universe?",
"answerText": [
{"text": "2005", "score": 5},
{"text": "2008", "score": 6},
{"text": "2010", "score": 7},
{"text": "2012", "score": 8},
]
},
{
"questionText": "What is the name of Thor’s hammer?",
"answerText": [
{"text": "Vanir", "score": 5},
{"text": "Mjolnir", "score": 6},
{"text": "Aesir", "score": 7},
{"text": "Norn", "score": 8},
],
},
{
"questionText":
"In the Incredible Hulk, what does Tony tell Thaddeus Ross at the end of the film?",
"answerText": [
{"text": "That he wants to study The Hulk", "score": 5},
{"text": "That he knows about S.H.I.E.L.D", "score": 6},
{"text": "That they are putting a team together", "score": 7},
{"text": "That Thaddeus owes him money", "score": 8},
],
},
{
"questionText":
"The Flerkens are a race of extremely dangerous aliens that resembles what?",
"answerText": [
{"text": "Cats", "score": 5},
{"text": "Ducks", "score": 6},
{"text": "Reptiles", "score": 7},
{"text": "Raccoons", "score": 8},
],
},
{
"questionText":
"Before becoming Vision, what is the name of Iron Man’s A.I. butler?",
"answerText": [
{"text": "H.O.M.E.R.", "score": 5},
{"text": "J.A.R.V.I.S.", "score": 6},
{"text": "A.L.F.R.E.D.", "score": 7},
{"text": "M.A.R.V.I.N.", "score": 8},
],
},
{
"questionText": "What's the real name of the Blank Panther?",
"answerText": [
{"text": "T’Challa", "score": 5},
{"text": "M’Baku", "score": 6},
{"text": "N’Jadaka", "score": 7},
{"text": "N’Jobu", "score": 8},
]
},
{
"questionText":
"What is the alien race Loki sends to invade Earth in The Avengers?",
"answerText": [
{"text": "The Chitauri", "score": 5},
{"text": "The Skrulls", "score": 6},
{"text": "The Kree", "score": 7},
{"text": "The Flerkens", "score": 8},
],
},
{
"questionText":
"Who was the last holder of the Space Stone before Thanos claims it for his Infinity Gauntlet?",
"answerText": [
{"text": "Thor", "score": 5},
{"text": "Loki", "score": 6},
{"text": "The Collector", "score": 7},
{"text": "Tony Stark", "score": 8},
],
},
{
"questionText":
"What fake name does Natasha use when she first meets Tony?",
"answerText": [
{"text": "Natalie Rushman", "score": 5},
{"text": "Natalia Romanoff", "score": 6},
{"text": "Nicole Rohan", "score": 7},
{"text": "Naya Rabe", "score": 8},
]
}
];
void _answerQuestion(int score) {
_totalScore = score;
setState(() {
//preventing app from exceeding range, so it won't crash
if (_questionIndex < _questions.length) {
_questionIndex ;
}
});
print("Answer Choosen!");
print(_totalScore);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Flutter App"),
centerTitle: true,
backgroundColor: Colors.black87,
),
body: (_questionIndex < _questions.length)
? Quiz(
answerQuestion:() => _answerQuestion(2),
questions: _questions,
questionIndex: _questionIndex)
: Center(
child: Text(
"You did it!",
style: TextStyle(fontSize: 40),
),
),
),
);
}
}
CodePudding user response:
var _questions = [
{
"questionText":
"What year was the first Iron Man movie released, kicking off the Marvel Cinematic Universe?",
"answerText": [
{"text": "2005", "score": 5},
{"text": "2008", "score": 6},
{"text": "2010", "score": 7},
{"text": "2012", "score": 8},
]
}
];
According to your _questions
data, (in your Quiz class) questions[questionIndex]["answerText"]
should be a List<Object>
and answer
should be a Map<String, Object>
and then you can extract the answer text
from the answerMap
.
class Quiz extends StatelessWidget {
final VoidCallback answerQuestion;
final List<Map<String, Object>> questions;
final int questionIndex;
Quiz({
@required this.answerQuestion,
@required this.questions,
@required this.questionIndex,
});
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Question(questions[questionIndex]["questionText"] as String),
...(questions[questionIndex]["answerText"] as List<Object>)
.map((answer) {
var answerMap = answer as Map<String, Object>;
return Answer(answerQuestion, answerMap['text'] as String);
}).toList(),
],
),
alignment: Alignment.center,
);
}
}