简体   繁体   English

出现错误“无法将参数类型‘JsObject’分配给参数类型‘BuildContext’”

[英]Getting error "The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'"

I tried to fix it but it didn't work for me.我试图修复它,但它对我不起作用。 I think I am missing something, it works in older version of Flutter but not in recent one.我想我遗漏了一些东西,它适用于 Flutter 的旧版本,但不适用于最新版本。

I am trying to set (onTap) function on an image to go to the next widget screen.我正在尝试将图像上的 (onTap) function 设置为 go 到下一个小部件屏幕。

Here is my code:这是我的代码:

    import 'dart:js';
    
    import 'package:flutter/material.dart';
    import 'package:zar/screen/category.dart';
    
    class TopCard extends StatefulWidget {
      const TopCard({Key? key}) : super(key: key);
    
      @override
      State<TopCard> createState() => _TopCardState();
    }
    
    // TOP CARD CLASS STARTS HERE
    class CardItem {
      final String urlImage;
      final String title;
      final String subTitle;
    
      const CardItem({
        required this.urlImage,
        required this.title,
        required this.subTitle,
      });
    }
    
    // TOP CARD WIDGETS STARTS HERE
    Widget topCard({
      required CardItem item,
    }) =>
        Container(
          width: 150,
          child: Column(
            children: [
              Expanded(
                child: AspectRatio(
                  aspectRatio: 2 / 2,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: Material(
                      child: Ink.image(
                        image: NetworkImage(item.urlImage),
                        child: InkWell(
                          onTap: () => Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => Categories(
                                item: item,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              const SizedBox(height: 4),
              Text(
                item.title,
                style: const TextStyle(
                    color: Color(0xff5e35b1),
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
              ),
              Text(
                item.subTitle,
                style: const TextStyle(
                  color: Colors.redAccent,
                ),
              ),
            ],
          ),
        );
    
    class _TopCardState extends State<TopCard> {
    // TOP CARD LIST VIEW STARTS HERE
      List<CardItem> items = const [
        CardItem(
          urlImage:
              'https://img.freepik.com/free-vector/pizza-melted-cartoon-illustration-flat-cartoon-style_138676-2876.jpg?size=338&ext=jpg',
          title: 'PIZZA',
          subTitle: '\$20',
        ),
        CardItem(
          urlImage:
              'https://img.freepik.com/free-vector/triangle-sandwich-cartoon-icon-illustration_368721-11.jpg?size=338&ext=jpg',
          title: 'SANDWICH',
          subTitle: '\$7.99',
        ),
        CardItem(
          urlImage:
              'https://thumbs.dreamstime.com/b/french-fries-icon-flat-vector-related-icon-long-shadow-web-mobile-applications-can-be-used-as-logo-pictogram-icon-90676285.jpg',
          title: 'FRIES',
          subTitle: '\$2.99',
        ),
        CardItem(
          urlImage:
              'https://i.pinimg.com/originals/42/de/53/42de53a97cac79ddcaee570c436a10e6.jpg',
          title: 'BURGER',
          subTitle: '\$5.99',
        ),
      ];
      @override
      Widget build(BuildContext context) {
        return Container(
          margin: const EdgeInsets.only(top: 20),
          height: 150,
          child: ListView.separated(
            scrollDirection: Axis.horizontal,
            itemCount: 4,
            separatorBuilder: (constext, _) => const SizedBox(width: 16),
            itemBuilder: (context, index) => topCard(
              item: items[index],
            ),
          ),
        );
      }
    }

But I get Error in (context)但是我在(上下文)中出错

    child: InkWell(
     onTap: () => Navigator.push(
     context,
      MaterialPageRoute(
       builder: (context) => Categories(
        item: item,
        ),
       ),
      ),
     ),

your helper method needs the context您的辅助方法需要context

 child: InkWell(
     onTap: () => Navigator.push(
     context, // HERE You need context
      MaterialPageRoute(
       builder: (context) => Categories(
        item: item,
        ),
       ),
      ),
     ),

So add it as a parameter所以将其添加为参数

// TOP CARD WIDGETS STARTS HERE // 顶级卡片小工具从这里开始

Widget topCard({
      required CardItem item,
      required BuildContext context
    }) => ...

and remove并删除

import 'dart:js'; // it gives you another context that you don't need 'JsObject' 

Another solution is to move topCard helper method within your state class, so you dont have to pass the context as an argument.另一种解决方案是在 state class 中移动topCard辅助方法,因此您不必将context作为参数传递。

I got the same error.我得到了同样的错误。 Flutter automatically imported dart.js library, which is giving the error. Flutter自动导入dart.js库,报错。 Go to the top of the file and remove this line: Go 到文件顶部并删除此行:

import 'dart:js'; 

暂无
暂无

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

相关问题 错误:无法将参数类型“JsObject”分配给参数类型“BuildContext” - Error: The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 参数类型“JsObject”不能分配给参数类型“BuildContext”。dart - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'.dart 无法将参数类型“ JsObject”分配给参数类型“ BuildContext” - The argument type 'JsObject' 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 参数类型“JsObject”不能分配给参数类型“BuildContext” - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 无法将参数类型“jsObject”分配给“buildContext”参数? - The argument type 'jsObject' can't be assigned to 'buildContext' parameter? 导航器错误在颤振导航器错误参数类型&#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' 使用简单代码时,无法将参数类型“JsObject”分配给参数类型“BuildContext” - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' while using simple code 类型“JsObject”不能分配给类型“BuildContext” - Type 'JsObject' can't be assigned to type 'BuildContext' 错误:无法将参数类型“Context”分配给参数类型“BuildContext” - Error : The argument type 'Context' can't be assigned to the parameter type 'BuildContext'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM