【问题标题】:In flutter: the necessary "required" keyword when extracting a widget在颤振中:提取小部件时必要的“必需”关键字
【发布时间】:2021-07-21 22:09:28
【问题描述】:

我正在开发一个应用程序,并在开始时提取了一个小部件以使我的代码整洁有序..

当我给contracter参数时,它会强制我添加“required”关键字,这在之前是不必要的!!

我怎么能忽略它?

class NewWidget extends StatelessWidget {
  NewWidget({this.text, this.onPress});
  final String text;
  final Function onPress;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Card(
        color: Colors.blueAccent,
        elevation: 5.0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: TextButton(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 100.0),
            child: Text(
              text,
              style: TextStyle(
                fontSize: 32.0,
                color: Colors.white,
              ),
            ),
          ),
          onPressed: onPress(),
        ),
      ),
    );
  }
}

这是我收到的通知..

参数'text'由于其类型不能有'null'的值,但隐含的默认值是'null'。尝试添加显式的非“null”默认值或“required”修饰符。

【问题讨论】:

  • 请分享您的代码,以便我们查看
  • 我已经分享了
  • 是的,现在命名参数应该包含在 dart 的空安全版本中。但是,如果您希望它是可选的,请使用 final String? text;
  • 没错,我希望它是可选的

标签: android flutter android-studio


【解决方案1】:

它来自 Dart 的 nullsafety,您的两个参数被定义为不可为空的变量,但它们在您的构造函数中是可选的,这意味着编译器无法推断您的 text 的类型是 String 还是 String?变量并且无法确定onPressFunction 还是Function?

您要么需要将参数作为required 传递,要么将它们声明为可为空并检查它们是否为null。以下是这两种情况的示例:

需要添加

class NewWidget extends StatelessWidget {
  NewWidget({required this.text, required this.onPress});
  final String text;
  final Function onPress;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Card(
        color: Colors.blueAccent,
        elevation: 5.0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: TextButton(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 100.0),
            child: Text(
              text,
              style: TextStyle(
                fontSize: 32.0,
                color: Colors.white,
              ),
            ),
          ),
          onPressed: onPress,
        ),
      ),
    );
  }
}

使用可为空的参数

class NewWidget extends StatelessWidget {
  NewWidget({this.text, this.onPress});
  final String? text;
  final Function? onPress;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15.0),
      child: Card(
        color: Colors.blueAccent,
        elevation: 5.0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: TextButton(
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 100.0),
            child: Text(
              text ?? "text String is null",
              style: TextStyle(
                fontSize: 32.0,
                color: Colors.white,
              ),
            ),
          ),
          onPressed: () => onPress!(),
        ),
      ),
    );
  }
}

【讨论】:

  • 这很有用,但我遇到了其他事情。我在“onPress”参数中添加的功能是导航器,但是当我添加它时,它没有进入目标页面!!!跨度>
猜你喜欢
  • 2021-02-14
  • 2021-03-03
  • 2021-03-17
  • 2021-11-13
  • 2020-12-07
  • 2019-10-23
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
相关资源
最近更新 更多