SFSA_Despair
SFSA_Despair

Reputation: 39

The element type '{0}' can't be assigned to the list type '{1}' flutter

I've been trying to follow a flutter course on udemy and in the code it gives me this error:

The element type 'Question' can't be assigned to the list type 'Widget'.

here's the code in main.dart:

    class _MyAppState extends State<MyApp> {
  int _qIndex = 0;

  void _ansQ() {
    setState(() {
      _qIndex++;
    });
    print('Answer Chosen');
  

   print(_qIndex);   }
 
   @override   Widget build(BuildContext context) {
     var questions = [
       'What\'s your favorite food?',
       'What\'s your favorite color?'
     ];
     return MaterialApp(
       home: Scaffold(
           appBar: AppBar(
             title: Text('My First Flutter App'),
           ),
           body: Column(
             children: [
               Question(questions[_qIndex]), //This is the line that gives me the error
               RaisedButton(child: Text('Answer 1'), onPressed: _ansQ),
               RaisedButton(
                 child: Text('Answer 2'),
                 onPressed: () => print('Answer 2 chosen')),
               RaisedButton(child: Text('Answer 3'), onPressed: _ansQ)
             ]
           )
        ),
     );   } }

And here is the code in the question.dart:

import 'package:/flutter/material.dart';

class Question extends StatelessWidget {
  final String questionText;
  const Question(this.questionText);

  @override
  Widget build(BuildContext context) {
    return Text(questionText);
  }
}

If so can you please help me?

Upvotes: 2

Views: 50

Answers (1)

Bigfoot
Bigfoot

Reputation: 357

try to ad as qus after the import like this

import 'package:'PATH'/Question.dart' as qus;

then use

Column(
             children: [
              qus.Question(questions[_qIndex]), //This is the line that gives me the error
               RaisedButton(child: Text('Answer 1'), onPressed: _ansQ),
               RaisedButton(
                 child: Text('Answer 2'),
                 onPressed: () => print('Answer 2 chosen')),
               RaisedButton(child: Text('Answer 3'), onPressed: _ansQ)
             ]

Upvotes: 3

Related Questions