简体   繁体   English

错误:返回类型“CategoryState”不是“Widget”,这是闭包上下文所要求的

[英]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 a Widget , as required by the closure's context.错误:根据闭包上下文的要求,返回类型CategoryState不是Widget

Page code页面代码

Navigation code导航代码

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-basicsMaterialPageRoute ZA2F2ED4F8EBC2CBB14C21A29DCZ40 的构建器 function 应该返回一个小部件Both StatelessWidget and StatefulWidget extend the Widget class and thus can be used here. StatelessWidgetStatefulWidget都扩展了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.

相关问题 返回类型“小部件?” 不是闭包上下文所要求的“小部件” - The return type 'Widget?' isn't a 'Widget', as required by the closure's context 根据闭包上下文的要求,返回类型“Person”不是“Widget” - The return type 'Person' isn't a 'Widget', as required by the closure's context 根据闭包上下文的要求,返回类型“Object”不是“Widget” - The return type 'Object' isn't a 'Widget', as required by the closure's context 返回类型“_MainPage”不是闭包上下文所要求的“Widget” - The return type '_MainPage' isn't a 'Widget', as required by the closure's context 错误:根据闭包上下文的要求,返回类型“NetworkImage”不是“Widget” - error: The return type 'NetworkImage' isn't a 'Widget', as required by the closure's context 错误消息:根据闭包上下文的要求,返回类型“void”不是“Widget” - Error Message: The return type 'void' isn't a 'Widget', as required by the closure's context 返回类型“SliverAppBar?” 不是&#39;列表<Widget> &#39;,根据闭包上下文的要求 - The return type 'SliverAppBar?' isn't a 'List<Widget>', as required by the closure's context 返回类型&#39;列表<ListTile> &#39; 不是闭包上下文所要求的 &#39;Widget&#39; - The return type 'List<ListTile>' isn't a 'Widget', as required by the closure's context 如何解决“返回类型'List<text> ? 不是闭包上下文所要求的“小部件”。”</text> - How to solve "The return type 'List<Text>?' isn't a 'Widget', as required by the closure's context." 返回类型 'Future<null> ' 不是闭包上下文所要求的“小部件”</null> - The return type 'Future<Null>' isn't a 'Widget', as required by the closure's context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM