[英]error: The return type 'CategoryState' isn't a 'Widget', as required by the closure's context
I Tried to put the route to Another Class on the Same Page and wrote this code As mentioned in the flutter Doc.我试图将路由放到同一页面上的另一个 Class 并编写此代码如 flutter 文档中所述。 I get this
我明白了
error: The return type
CategoryState
isn't aWidget
, as required by the closure's context.错误:根据闭包上下文的要求,返回类型
CategoryState
不是Widget
。
class CategoryState is not a widget it's just a Dart class you want to navigate to Category() class CategoryState 不是一个小部件,它只是一个 Dart class 您想要导航到 Category()
According to the documentation: https://flutter.dev/docs/cookbook/navigation/navigation-basics , the builder function of the MaterialPageRoute
class should return a widget.根据文档: https://flutter.dev/docs/cookbook/navigation/navigation-basics ,
MaterialPageRoute
ZA2F2ED4F8EBC2CBB14C21A29DCZ40 的构建器 function 应该返回一个小部件Both StatelessWidget
and StatefulWidget
extend the Widget
class and thus can be used here. StatelessWidget
和StatefulWidget
都扩展了Widget
class,因此可以在这里使用。
But in your case the CategoryState
class does not extend any of them, and it's the reason you are having that error.但是在您的情况下,
CategoryState
class 没有扩展它们中的任何一个,这就是您遇到该错误的原因。
You need to use the Category
class instead.您需要改用
Category
class。
It should then look like:然后它应该看起来像:
PAGE:页:
class Category extends StatefulWidget {
@override
_CategoryState createState() => _CategoryState();
}
class _CategoryState extends State<Category> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(child: Text("Music")),
Container(child: Text("Projects")),
Container(child: Text("Essay")),
],
);
}
}
NAVIGATION导航
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Category())
);
Furthermore, I would advise you to add a suffix to this class name (eg CategoryScreen) in order to avoid name conflicts in case you have another class (eg a model) with the same name.此外,我建议您为此 class 名称(例如 CategoryScreen)添加一个后缀,以避免名称冲突,以防您有另一个 class(例如模型)具有相同的名称。
Home my answer helps!回家我的回答有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.