To create this app I navigated to the project folder then type this command on the terminal
flutter create adv_quiz_app
// flutter create <app_name>
After this is created, navigate to adv_quiz_app
and to start/launch your Linux app locally run:
flutter run
A new Flutter project.
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter development, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
Every Flutter Widget has a built-in lifecycle: A collection of methods that are automatically executed by Flutter (at certain points of time).
There are three extremely important (stateful) widget lifecycle methods:
- initState(): Executed by Flutter when the StatefulWidget's State object is initialized
- build(): Executed by Flutter when the Widget is built for the first time AND after setState() was called
- dispose(): Executed by Flutter right before the Widget will be deleted (e.g., because it was displayed conditionally)
Read more on control flow collections
final myList = [
1,
2,
if (condition) // Note: no curly braces
3
else
4
];
In this example, the number 3 will only be added to myList if condition was met (condition can be true or false or a check that yields true or false - e.g., day == 'Sunday')
Another alternative is a ternary expression
final myList = [
1,
2,
condition ? 3 : 4
];
-
== operator checks for value equality
-
!= to check for inequality (randomNumber != 5 expects randomNumber to NOT be 5, i.e., to be any other value)
-
> to check for the value on the left to be greater than the value on the right (randomNumber > 5 yields true if randomNumber is greater than 5)
-
>= to check for the value on the left to be greater than or equal to the value on the right (randomNumber >= 5 yields true if randomNumber is greater than 5 or equals 5)
-
< to check for the value on the left to be smaller than the value on the right (randomNumber < 5 yields true if randomNumber is smaller than 5)
-
<= to check for the value on the left to be smaller than or equal to the value on the right (randomNumber <= 5 yields true if randomNumber is smaller than 5 or equals 5)
The following function of getSummaryData
can be re-written as a getter as shown further below.
class ResultsScreen extends StatelessWidget {
const ResultsScreen({
super.key,
required this.chosenAnswers,
required this.onRestart,
});
final List<String> chosenAnswers;
final void Function() onRestart;
List<Map<String, Object>> getSummaryData() {
final List<Map<String, Object>> summary = [];
for (var i = 0; i < chosenAnswers.length; i++) {
summary.add(
{
'question_index': i,
'question': questions[i].text,
'correct_answer': questions[i].answers[0],
'user_answer': chosenAnswers[i],
},
);
}
return summary;
}
The get
keyword is used and the parentheses used earlier on are omitted
class ResultsScreen extends StatelessWidget {
const ResultsScreen({
super.key,
required this.chosenAnswers,
required this.onRestart,
});
final List<String> chosenAnswers;
final void Function() onRestart;
List<Map<String, Object>> get summaryData { // changed
final List<Map<String, Object>> summary = [];
for (var i = 0; i < chosenAnswers.length; i++) {
summary.add(
{
'question_index': i,
'question': questions[i].text,
'correct_answer': questions[i].answers[0],
'user_answer': chosenAnswers[i],
},
);
}
return summary;
}
@override
Widget build(BuildContext context) {
final numTotalQuestions = questions.length;
final numCorrectQuestions = summaryData.where((data) { // changed
return data['user_answer'] == data['correct_answer'];
}).length;
return ...rest of the code
}