I have 3 Lists in my Flutter Project; How can I collect them in one list using Class and constructor in Flutter ?
my three lists;
- one has three question
- second has image
- third has true or false
I want to collect them in one list.
I tried:
class Question {
late String questionText;
late String questionImage;
late bool questionAnswer;
Question({String q, String i, bool a}) {
questionText = q;
questionImage = i;
questionAnswer = a;
}
}
it shows error in this line
I have 3 Lists in my Flutter Project; How can I collect them in one list using Class and constructor in Flutter ?
my three lists;
- one has three question
- second has image
- third has true or false
I want to collect them in one list.
I tried:
class Question {
late String questionText;
late String questionImage;
late bool questionAnswer;
Question({String q, String i, bool a}) {
questionText = q;
questionImage = i;
questionAnswer = a;
}
}
it shows error in this line
Share Improve this question edited Feb 18 at 4:21 Ruchit 2,7301 gold badge14 silver badges27 bronze badges asked Feb 17 at 22:43 Mostafa IsmaelMostafa Ismael 192 bronze badges New contributor Mostafa Ismael is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 4 |1 Answer
Reset to default 3You can implement your class like this:
class Question {
late String? questionText;
late String? questionImage;
late bool? questionAnswer;
Question({
required this.questionText,
required this.questionImage,
required this.questionAnswer,
});
}
Here, you may even remove the late
modifiers to the class instance variables. If you decide to also remove the required
keyword, a null
check may be necessary during manipulation of the values (also needed for when instance variable typed as nullable e.g. String?
).
You can use the class to collect and the information like this:
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Question myQuestion = Question(
questionText: "Is this a question?",
questionImage: "path/to/image.png",
questionAnswer: false,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Example")),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: [
Text("QT: ${myQuestion.questionText}"),
Text("QI: ${myQuestion.questionImage}"),
Text("QA: ${myQuestion.questionAnswer}"),
],
),
),
),
);
}
}
The full code for the demo example:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(home: const MyHomePage());
}
}
class Question {
late String? questionText;
late String? questionImage;
late bool? questionAnswer;
Question({
required this.questionText,
required this.questionImage,
required this.questionAnswer,
});
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Question myQuestion = Question(
questionText: "Is this a question?",
questionImage: "path/to/image.png",
questionAnswer: false,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Example")),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: [
Text("QT: ${myQuestion.questionText}"),
Text("QI: ${myQuestion.questionImage}"),
Text("QA: ${myQuestion.questionAnswer}"),
],
),
),
),
);
}
}
Hope this helps.
List<Question>
to represent the list. If you're having problems creating the list, show the code you tried (click theedit
link under your question to edit it) and the error you get. – Frank van Puffelen Commented Feb 18 at 2:40