首页 > 解决方案 > 元素类型“问题”不能分配给列表类型“小部件”

问题描述

我不能使用自己的Class/Widget Question()from question.dartto main.dart(ERR Location 3rd line after body):

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

标签: flutterdartconstructorwidget

解决方案


使用插值可以解决一些错误并在您的 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
  }
}

在这里输出


推荐阅读