未实现错误(颤动)

UnimplementedError (FLUTTER)

我对使用 Flutter 进行编码非常陌生,并且按照这门课程让我创建有状态的小部件。问题是当我这样做时,我得到了这个 throw UnimplementedError(); 它应该 return 为空。我不知道我做错了什么。

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

谢谢,

再见!

createState() 方法包含 TODO 评论:

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

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

然而,throw UnimplementedError() 使提醒变得不那么友好,迫使我们完成 TODO 任务并删除 UnimplementedError() 行以使其生效:

MyAppState createState() => MyAppState();