【问题标题】:The element type 'Question' can't be assigned to the list type 'Widget'元素类型“问题”不能分配给列表类型“小部件”
【发布时间】:2021-01-06 07:28:29
【问题描述】:

我不能使用我自己的Class/Widget Question()question.dartmain.dart(ERR 位置在正文之后的第 3 行):

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
  }
}

这里没有错误,我只是在我的 main.dart 文件上使用 Question() 时出错

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

【问题讨论】:

    标签: flutter dart constructor widget


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以添加行程运算符来检查从 Question 类返回的 Widget 是否为空。发生这种情况是因为 Column 的子小部件不能为空。

       body: Column(
            children: [
              Question(questions[questionIndex]) == null
                  ? SizedBox()
                  : Question(questions[questionIndex]),
      

      这样,如果从 Question 类返回的小部件为 null,那么 Column 将创建一个 SizedBox 而不是 Container + Text。

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 2019-11-29
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多