简体   繁体   English

Flutter Navigator“无法将参数类型'Context'分配给参数类型'BuildContext'”

[英]Flutter Navigator “argument type 'Context' can't be assigned to the parameter type 'BuildContext'”

I am trying to pass my menuButton category Names to the next page and have that set the state of the next page on my categoryScreens.我试图将我的 menuButton 类别名称传递到下一页,并在我的 categoryScreens 上设置下一页的状态。

Currently I am receiving a red squiggly line with a note of "argument type 'Context' can't be assigned to the parameter type 'BuildContext'"目前我收到一条红色波浪线,上面写着“无法将参数类型‘Context’分配给参数类型‘BuildContext’”

import 'package:flutter/material.dart';
import 'package:boardwalk/Screens/categoryScreen.dart';
import 'package:boardwalk/Widgets/headerCategory.dart';
import 'package:path/path.dart';

class homeMenu extends StatelessWidget {
  const homeMenu({Key key}) : super(key: key);
  Padding menuButton(String category, IconData categoryIcon) {
    return Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Container(
          width: 95,
          child: InkWell(
            onTap: ()=>
              Navigator.of(context).push(MaterialPageRoute(builder: (context) => categoryScreen()));
            ,
            child: Column(
              children: <Widget>[
                Center(
                  child: Container(
                    height: 70,
                    width: 70,
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: [Colors.deepPurple, Colors.deepPurpleAccent],
                      ),
                      shape: BoxShape.circle,
                    ),
                    child: Icon(
                      categoryIcon,
                      size: 40.0,
                      color: Colors.white,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(6.0),
                  child: Text(
                    category,
                    style: TextStyle(
                      color: Colors.deepPurpleAccent,
                      fontSize: 14,
                    ),
                  ),
                )
              ],
            ),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 8.0),
      child: Container(
        height: 100,
        width: 400,
        child: ListView(scrollDirection: Axis.horizontal, children: <Widget>[
          menuButton('Home', Icons.home),
          menuButton('Eat', Icons.restaurant_menu),
          menuButton('Shop', Icons.store),
          menuButton('Travel', Icons.airplanemode_active),
          menuButton('Play', Icons.local_activity),
          menuButton('Service', Icons.build),
        ]),
      ),
    );
  }
}

I am not sure what to be passing in the navigator if it does not like context, I am completely unsure of what is expecting at this point.如果导航器不喜欢上下文,我不确定在导航器中传递什么,我完全不确定此时会发生什么。

Actually, the issue is caused by importing the path.dart package.实际上,问题是由导入 path.dart 包引起的。 To fix this, change context to this.context要解决此问题,请将上下文更改为 this.context

我只是将上下文更改为 this.context 并在寻找解决方案 2 小时后解决了问题!

A Widget does not have access to the BuildContext outside the build method. Widget 无权访问build方法之外的BuildContext If you are creating the a reusable Widget function like menuButton here, then you have to pass in the BuildContext instance you receive in the build method.如果您正在创建一个可重用的 Widget 函数,例如这里的menuButton ,那么您必须传入您在build方法中收到的BuildContext实例。

So your code becomes,所以你的代码变成,

class homeMenu extends StatelessWidget {
  const homeMenu({Key key}) : super(key: key);

  Padding menuButton(BuildContext context, String category, IconData categoryIcon, BuildContext context) {
    return Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        ...
        );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 8.0),
      child: Container(
        height: 100,
        width: 400,
        child: ListView(scrollDirection: Axis.horizontal, children: <Widget>[
          menuButton(context, 'Home', Icons.home),
          ...
        ]),
      ),
    );
  }
}

If "import packages" didn't help means you should try below statement.如果“导入包”没有帮助,则意味着您应该尝试以下语句。

Declare BuildContext context;声明BuildContext 上下文; which is a parameter.这是一个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Argument 类型 &#39;Context&#39; 不能分配给参数类型 &#39;BuildContext&#39; - Flutter - The Argument type 'Context' can't be assigned to the parameter type 'BuildContext' - Flutter 导航器错误在颤振导航器错误参数类型&#39;JsObject&#39;不能分配给参数类型&#39;BuildContext&#39; - navigator error in flutter navigator error The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 错误:无法将参数类型“Context”分配给参数类型“BuildContext” - Error : The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 错误:无法将参数类型“Context”分配给参数类型“BuildContext” - error: The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 无法将参数类型“Context”分配给参数类型“BuildContext” - The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 参数类型“JsObject”不能分配给参数类型“BuildContext”。 - flutter - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'. - flutter 参数类型“BuildContext?” 不能分配给参数类型“BuildContext” - The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext' 错误:[dart] 无法将参数类型“Context”分配给参数类型“BuildContext”。 [argument_type_not_assignable] - error: [dart] The argument type 'Context' can't be assigned to the parameter type 'BuildContext'. [argument_type_not_assignable] 我在 context 上有错误。参数类型“Context”不能分配给参数类型“BuildContext” - I have error on context..The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 参数类型“上下文”不能分配给参数类型“BuildContext”。dartargument_type_not_assignable) - The argument type 'Context' can't be assigned to the parameter type 'BuildContext'.dartargument_type_not_assignable)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM