flutter - 元素类型 'Question'无法分配给列表类型 'Widget'

标签 flutter dart constructor widget

我无法使用自己的Class/Widget Question()(从question.dartmain.dart)(错误代码位于正文后的第三行):

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var questionIndex = 0;
  // State of function that process data to UI
  void _answerQuestion() {
    setState(() {
      questionIndex = questionIndex + 1;
    });
    print(questionIndex);
  }

  void _pangBawas() {
    setState(() {
      questionIndex = questionIndex - 1;
    });
    print(questionIndex);
  }

  // End of State
  // This section is responsible for builing UI
  @override
  Widget build(BuildContext context) {
    var questions = [
      'What\'s your favorite color?',
      'What\'s your favorite animal?',
      'What\'s your favorite food?',
      'What\'s your favorite color?',
      'What\'s your favorite dress?',
      'What\'s your favorite position?',
    ];
    // Start of UI
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Column(
          children: [
            Question( //ERROR: The element type 'Question' can't be assigned to the list type 'Widget'.
              questions[questionIndex],
            ),
            RaisedButton(
              child: Text('Answer 1'),
              onPressed: _answerQuestion,
            ),
            RaisedButton(
              child: Text('Answer 2'),
              onPressed: () => _pangBawas,
            ),
            RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  //....
                  print('Hi');
                }),
          ],
        ),
      ),
    );
    // End of UI
  }
}
这里没有错误,我只在Question()文件上使用main.dart出现错误
import 'package:/flutter/material.dart';

class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        questionText,
      ),
    );
  }
}

最佳答案

利用插值可以解决一些错误,并在UI中向您显示错误。下面的代码可以正常工作:

import 'package:flutter/material.dart';

class Question extends StatelessWidget {
  final String questiontext;
  Question(this.questiontext);
  @override
  Widget build(BuildContext context) {
    return Text("$questiontext");
  }
}

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var questionIndex = 0;
  // State of function that process data to UI
  void _answerQuestion() {
    setState(() {
      questionIndex = questionIndex + 1;
    });
    print(questionIndex);
  }

  void _pangBawas() {
    setState(() {
      questionIndex = questionIndex - 1;
    });
    print(questionIndex);
  }

  // End of State
  // This section is responsible for builing UI
  @override
  Widget build(BuildContext context) {
    var questions = [
      'What\'s your favorite color?',
      'What\'s your favorite animal?',
      'What\'s your favorite food?',
      'What\'s your favorite color?',
      'What\'s your favorite dress?',
      'What\'s your favorite position?',
    ];
    // Start of UI
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: Column(
          children: [
            Question(
              //ERROR: The element type 'Question' can't be assigned to the list type 'Widget'.
              "${questions[questionIndex]}",
            ),
            RaisedButton(
              child: Text('Answer 1'),
              onPressed: _answerQuestion,
            ),
            RaisedButton(
              child: Text('Answer 2'),
              onPressed: () => _pangBawas,
            ),
            RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  //....
                  print('Hi');
                }),
          ],
        ),
      ),
    );
    // End of UI
  }
}

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

相关文章:

dart - 使用 BLoC 处理导航的正确方法

java - 如何在android中实例化一个类,它也是一个 Activity ?

android - 运行 Flutter App 时出现 java.lang.NullPointerException

android - 无法在 Android 嵌入式浏览器上使用 Facebook 登录

recursion - 在递归期间为变量赋值

dart - 如何实现像 Youtube 和 Instagram 应用程序中那样的导航堆栈?

java - 从字节数组创建对象(使用构造函数)

c++ - 避免使用 "child"延迟 `operator<<` 对象构造

dart - Flutter:从子小部件设置父小部件状态

android - 无法在 Flutter 的 EventChannel 上打开事件流(指定为非 null 的参数为 null)