I want to print first element in the list "first ques"
but I’m getting different sort of errors tried using final also, then all sort of errors.
class _QuizPageState extends State<QuizPage> {
List<Widget> scoreKeeper = [];
List<String> questions = ["First ques", "seacond ques", "third ques"];
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
"example text"
questions[0], //why its not displaying "first ques"
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
2
Answers
try this
the reason you are getting error is that the Text widget takes only one string and you are passing two string to it.
First, you should remove
const
from theExpanded
widget because you’re using a variable inside onText
which isquestions[0]
.And I want to mention that its better to use
questions.first
for readability proposes and check if the list is empty before it to prevent possible errors.So Here’s the final result: