skip to Main Content

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


  1. try this

      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( 
                      questions[0], //why its not displaying "first ques"
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 25.0,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
              ),
    

    the reason you are getting error is that the Text widget takes only one string and you are passing two string to it.

    Login or Signup to reply.
  2. First, you should remove const from the Expanded widget because you’re using a variable inside on Text which is questions[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:

    Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            flex: 5,
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: Center(
                child: Text(
                  questions.first,
                  //why its not displaying "first ques"
                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    fontSize: 25.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search