我的无状态小部件触发了我无法分配的错误消息

Posted

技术标签:

【中文标题】我的无状态小部件触发了我无法分配的错误消息【英文标题】:My stateless widget triggers an error message that I cannot assign 【发布时间】:2020-07-26 21:02:47 【问题描述】:

我经常在这里找到非常快速和非常称职的帮助。并希望你能再次帮助我。 我收到了三天的错误消息,我无法解决。 我在一个小视频中向您解释了这个问题。 视频链接为:“https://drive.google.com/file/d/1T9uOnEaNp5W6_kcO6eV9o64Fp3sRkB3f/view?usp=sharing”

我真的希望你看到我没有看到的错误!

这是Foodcard的代码,根据Flutter应该会导致错误:

The method '-' was called on null.
Receiver: null
Tried calling: -(30.0)
The relevant error-causing widget was: 
  FoodCard file:///C:/Users/stefa/androidStudioProjects/cronum_app_web%20-%20Kopie/lib/Components/ProviderComponents.dart:298:9

食品卡:

class FoodCard extends StatelessWidget 
  FoodCard(
    @required this.description,
    @required this.imagePath,
    @required this.price,
    @required this.foodName,
    @required this.foodItem,
    @required this.increaseCallback,
    @required this.decreaseCallback,
    this.count = 0,
  );

  final double radius = 40;
  static double listViewHeight;
  final double margin = 15;
  final double containerHeight = 130;
  final int count;
  final String imagePath;
  final String foodName;
  final String price;
  final String description;
  final FoodItem foodItem;
  final Function increaseCallback;
  final Function decreaseCallback;

  @override
  Widget build(BuildContext context) 
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10, vertical: margin),
      width: 220,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(radius),
        gradient: LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.topRight,
          colors: ([
            gradientColor1.withOpacity(opacityOfHeader),
            primaryColor.withOpacity(opacityOfHeader),
            gradientColor2.withOpacity(opacityOfHeader),
          ]),
        ),
        boxShadow: [
          BoxShadow(
            blurRadius: 8,
            color: Colors.black.withOpacity(0.3),
            offset: Offset(5, 5),
          ),
        ],
        color: Colors.white,
      ),
      child: Column(
        children: <Widget>[
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              Column(
                children: <Widget>[
                  SizedBox(
                    height: listViewHeight - margin * 2 - containerHeight,
                  ),
                  Container(
                    height: containerHeight,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(radius),
                          topRight: Radius.circular(radius),
                          bottomLeft: Radius.circular(radius),
                          bottomRight: Radius.circular(radius),
                        ),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black.withOpacity(0.2),
                              offset: Offset(0, -5),
                              blurRadius: 8)
                        ],
                        color: Colors.white),
                    child: Column(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.only(top: 40),
                          child: Center(
                            child: Text(
                              foodName,
                              style: TextStyle(
                                color: Colors.black,
                                fontSize: 17,
                                fontWeight: FontWeight.w900,
                              ),
                            ),
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(
                              horizontal: 20, vertical: 15),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              InkWell(
                                onTap: () 
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => DetailPage(
                                        foodItem: foodItem,
                                        increaseCount: increaseCallback,
                                        decreaseCount: decreaseCallback,
                                      ),
                                    ),
                                  );
                                ,
                                child: Container(
                                  height: 30,
                                  width: 80,
                                  decoration: BoxDecoration(
                                    boxShadow: [
                                      BoxShadow(
                                          offset: Offset(5, 5),
                                          color: Colors.black.withOpacity(0.3),
                                          blurRadius: 8)
                                    ],
                                    color: buttonColor,
                                    borderRadius: BorderRadius.circular(25),
                                  ),
                                  child: Padding(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: 7, vertical: 5),
                                    child: Center(
                                      child: Text(
                                        'Details',
                                        style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 16,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              Container(
                                color: Colors.grey.withOpacity(0.5),
                                height: 25,
                                width: 1,
                              ),
                              Container(
                                height: 30,
                                width: 80,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(25),
                                  color: buttonColor,
                                  boxShadow: [
                                    BoxShadow(
                                      blurRadius: 8,
                                      color: Colors.black.withOpacity(0.3),
                                      offset: Offset(5, 5),
                                    ),
                                  ],
                                ),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  children: <Widget>[
                                    InkWell(
                                      onTap: decreaseCallback,
                                      child: Container(
                                        height: 22,
                                        width: 22,
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          color: buttonColor,
                                        ),
                                        child: Center(
                                          child: Icon(
                                            Icons.remove,
                                            color: Colors.white,
                                            size: 22,
                                          ),
                                        ),
                                      ),
                                    ),
                                    Text(
                                      count.toString(),
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 16,
                                        fontWeight: FontWeight.w900,
                                      ),
                                    ),
                                    InkWell(
                                      onTap: increaseCallback,
                                      child: Container(
                                        height: 22,
                                        width: 22,
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(15),
                                          color: Colors.white,
                                        ),
                                        child: Center(
                                          child: Icon(
                                            Icons.add,
                                            color: buttonColor,
                                            size: 22,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Positioned(
                top: 30,
                child: InkWell(
                  onTap: () 
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => DetailPage(
                          foodItem: foodItem,
                          increaseCount: increaseCallback,
                          decreaseCount: decreaseCallback,
                        ),
                      ),
                    );
                  ,
                  child: Image(
                    height: listViewHeight / 2,
                    width: listViewHeight / 2,
                    image: AssetImage(imagePath),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  

如果不是因为 FoodCard,我创建 Foodcard 列表的功能也可能是问题所在。 我对错误消息感到惊讶,因为它非常不具体。在我自动创建列表之前,一切都很顺利。


  List<Widget> buildEntdeckenCards() 
    List<Widget> foodCardList = [];
    for (FoodItem foodItem in top10) 
      print(foodItem.foodName);
      foodCardList.add(
        FoodCard(
          description: foodItem.description,
          foodName: foodItem.foodName,
          foodItem: foodItem,
          price: foodItem.price,
          imagePath: foodItem.imagePath,
          decreaseCallback: () 
            decreaseCount(foodItem);
          ,
          increaseCallback: () 
            increaseCount(foodItem);
          ,
        ),
      );
    
    return foodCardList;
  

【问题讨论】:

您是否检查过 ProviderComponents.dart 文件第 298:9 行的值是 30.0 而不是 30? 嘿,感谢您的回复并查看我的代码。在图片中,您可以看到适当位置的内容。不幸的是,我认为这没有任何意义...... 图片:“drive.google.com/file/d/187ViDc2ncSzK7hHBa7p2qDSWqaYuaqMn/…” 如果它解决了你的问题,也请给一个upvote以获得灵感。 【参考方案1】:

我认为您的 listViewHeight 变量为空,而您正尝试在表达式中使用它

  SizedBox(height: listViewHeight - margin * 2 - containerHeight,),

我认为这就是您看到以下错误 The method '-' was called on null 的原因。在使用之前为listViewHeight 赋值。

【讨论】:

你是对的!重构代码的时候没有认真工作,搞错了!谢谢! 非常感谢您,我总能在这里找到帮助。你是最棒的!

以上是关于我的无状态小部件触发了我无法分配的错误消息的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 状态管理(BloC):无状态与有状态小部件

无法将 onTap 字段传递给无状态小部件

如何使用标准 Azure 逻辑应用的无状态工作流可靠地处理 Azure 服务总线消息

Flutter:何时创建无状态或有状态页面?

http的无状态无连接

如何在有状态或无状态小部件之外访问 BuildContext?