【发布时间】:2021-04-07 00:12:57
【问题描述】:
我是使用 Flutter 编码的超级新手,我学习了这门课程,让我创建有状态的小部件。问题是当我这样做时,我得到这个 throw UnimplementedError(); 它应该返回 null。我不知道我做错了什么。
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,
)
],
),
),
);
}
}
谢谢,
Ciao!
【问题讨论】:
-
您收到 UnimplementedError 错误,因为您在 createState 中抛出异常。
标签: visual-studio flutter widget throw stateful