android - 元素类型 '{0}'无法分配给列表类型 '{1}' flutter

标签 android flutter dart visual-studio-code widget

我一直在尝试遵循 udemy 上的 flutter 类(class),但在代码中它给了我这个错误:

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

这是 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)
             ]
           )
        ),
     );   } }

这是 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);
  }
}

如果可以的话,你能帮我吗?

最佳答案

尝试在导入后添加as qus,如下所示

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

然后使用

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)
             ]

关于android - 元素类型 '{0}'无法分配给列表类型 '{1}' flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69448916/

相关文章:

java - "New Game"安卓游戏进度删除按钮

javascript - 我不能在 Android 上使用嵌入式 &lt;style&gt; CSS 吗?

android - 如何使用推送通知同步 SQLite 和 MySQL 数据库?

flutter - 如何将示例项目添加到 Flutter 包中?

android - 如何在Flutter中打印列表后防止自动向下滚动

android - WearableListenerService onCreate() 从未调用过

flutter - 如何在flutter的custom_radio_grouped_button包中更改fontsize?

flutter - 如何在 flutter 中显示更多的数字选择器列表元素?

dart - Angular Dart中的工具提示

css - 如何调整输入的长度