未实现错误(FLUTTER)

时间:2020-12-30 13:35:18

标签: visual-studio flutter widget throw stateful

我是使用 Flutter 编码的超级新手,我正在学习这门课程,让我可以创建有状态的小部件。问题是当我这样做时,我得到了这个 throw UnimplementedError(); 它应该返回空值。我不知道我做错了什么。

my code:

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    throw UnimplementedError();
  }
}
class MyAppState extends State<MyApp> {
  var questionIndex = 0;

  void answerQuestion() {
    setState(() {
      questionIndex = questionIndex + 1;
    });
    print(questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    var questions = [
      "What's your favourite color?",
      "What's your favourite animal?",
    ];
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            "My First App",
          ),
        ),
        body: Column(
          children: [
            Text(
              questions[questionIndex],
            ),
            RaisedButton(
              child: Text("Answer 1"),
              onPressed: answerQuestion,
            ),
            RaisedButton(
              child: Text("Answer 2"),
              onPressed: answerQuestion,
            ),
            RaisedButton(
              child: Text("Answer 3"),
              onPressed: answerQuestion,
            )
          ],
        ),
      ),
    );
  }
}

谢谢,

呸!

1 个答案:

答案 0 :(得分:3)

createState() 方法包含一个 TODO 注释:

@override
State<StatefulWidget> createState() {
  // TODO: implement createState
  throw UnimplementedError();
}

这条评论是善意的提醒,我们还有事情要做。

然而,throw UnimplementedError() 使提醒不太友好,迫使我们完成 TODO 任务并删除 UnimplementedError() 行以使其工作:

MyAppState createState() => MyAppState();
相关问题