简体   繁体   English

我正在尝试使用颤振制作 Bmi 计算器,但出现了很多错误

[英]I am trying to make the Bmi calculator using flutter but got many errors


import 'package:flutter/material.dart';

class ReusableCard extends StatelessWidget {
  ReusableCard({@required this.colour, this.cardChild, this.onPress}); // i am getting errors here
  final Color colour ;
  final Widget cardChild;
  final Function onPress;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress(),
      child: Container(
//if box decoration is there in the code then the color property should go in the box decoration property
        decoration: BoxDecoration(
            color:colour,
            borderRadius: BorderRadius.circular(15.0)
        ),
        margin: EdgeInsets.all(15.0),
      ),
    );
  }
}

i am getting error: The parameter 'onPress', 'cardChild', 'colour' can't have a value of 'null' because of its type, but the implicit default value is 'null'.我收到错误消息:参数“onPress”、“cardChild”、“color”因其类型而不能具有“null”值,但隐含的默认值为“null”。 in the dart analysis在飞镖分析中

The error you facing is caused by null-safety, you may want to change your types from:您面临的错误是由空安全引起的,您可能希望将类型从以下位置更改:

final Color colour ;
final Widget cardChild;
final Function onPress;

//to

final Color? colour ;
final Widget? cardChild;
final Function? onPress;

But I recommend you to put them all in the constructor with the decorator @required但是我建议您将它们全部放在带有装饰器@required的构造函数中

You can learn more about null-safety syntaxes on the official documentation:您可以在官方文档中了解有关空安全语法的更多信息:
https://flutter.dev/docs/null-safety https://flutter.dev/docs/null-safety

I faced the same error, even using the @required decorator wasn't enough to solve the issue, What solved it for me was that i had to disable the null-safety for the project and that's by downgrading the flutter version from ">=2.12.0 <3.0.0" to ">=2.11.0 <3.0.0" through modifying the pubspec.yaml file.我遇到了同样的错误,即使使用@required装饰器也不足以解决问题,为我解决的问题是我必须禁用项目的空安全性,这是通过从 ">=降级颤振版本2.12.0 <3.0.0" 到 ">=2.11.0 <3.0.0" 通过修改pubspec.yaml文件。 Specifically under dependencies -> sdk.特别是在依赖项-> sdk 下。

Keep in mind that by downgrading the project's flutter version will disable the entire usage of null-safety including the "?"请记住,通过降级项目的颤振版本将禁用 null-safety 的整个使用,包括“?” checking operator检查运算符

Here's a good read from Flutter Docs to better understand the null-safety feature in the Dart language : https://dart.dev/null-safety#enable-null-safety为了更好地理解 Dart 语言中的 null-safety 特性,请阅读 Flutter Docs 的精彩内容: https ://dart.dev/null-safety#enable-null-safety

This is a good comprehensive article as well https://petercoding.com/flutter/2021/03/04/using-null-safety-in-dart/这也是一篇很好的综合文章https://petercoding.com/flutter/2021/03/04/using-null-safety-in-dart/

I added 'required' to the constructor for onPress.我在 onPress 的构造函数中添加了“必需”。 However, I could not directly assign it to the onTap: part of the GestureDetector widget.但是,我不能直接将它分配给 onTap: GestureDetector 小部件的一部分。 So I called it from a method that did match the expected method signature for onTap.所以我从一个与预期的 onTap 方法签名匹配的方法中调用它。 In the end this is the (inelegant) solution that worked for me:最后,这是对我有用的(不优雅的)解决方案:

class ReusableCard extends StatelessWidget {

ReusableCard({required this.colour, this.cardChild, required this.onPress});

final Color colour;
final Widget? cardChild;
final Function onPress;

void onPressAction() {
    onPress();
}

@override
Widget build(BuildContext context) {
    return GestureDetector(
    onTap: onPressAction,
    child: Container(
      child: cardChild,
      margin: EdgeInsets.all(15.0),
      decoration: BoxDecoration(
        color: colour,
        borderRadius: BorderRadius.circular(10.0)
      ),
    ),
  );
} 

暂无
暂无

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

相关问题 我正在尝试使用 Flutter 制作计算器应用程序的副本 UI,但我遇到了一些问题 - I am trying to make a replica UI of a calculator app using Flutter and I am stuck in a few things 我正在尝试在 flutter 中制作一个测验应用程序,以极客教程作为参考。我有以下错误 - I am trying to make a quiz app in flutter with geeks for geeks tutorial as reference.. i have following errors 我正在尝试使用 python (flask) 作为后端在 Flutter 中建立连接 - I am trying to make a connection in Flutter using python (flask) for back end 我正在尝试在异步 function 中运行两个 await 和一个 navigator.push,我在 flutter 中遇到了这个错误 - I am trying to run two await and a navigator.push in an async function, and i got this error in flutter 尝试在 flutter 上发出获取请求时出错 - I am having an error while trying to make a get request on flutter 在 Flutter 中,我正在尝试使用以下 json 创建依赖下拉列表 - In Flutter, I am trying to make a dependent dropdown with the following json 我正在尝试使用 https.MultipartRequest 在 Flutter 中调用 API - I am trying to make a call to an API with https.MultipartRequest in Flutter 我正在尝试运行我的 flutter 应用程序,但出现这些错误? - I am trying to run my flutter app and I am getting these errors? 升级到 flutter 3.3.0 后出现这些错误 - After upgrading to flutter 3.3.0 I got these errors 我正在尝试在 flutter 应用程序中实现底部导航栏,但我的源代码中不断出现错误 - I am trying to implement a bottom navigation bar in a flutter app but I keep getting errors in my source code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM