【问题标题】:Flutter/Dart Null Safety on _formKey.currentState.validate()?_formKey.currentState.validate() 上的 Flutter/Dart Null 安全性?
【发布时间】:2021-05-18 18:54:28
【问题描述】:

我刚刚在 pubspec.yaml 中更新了我的最小 sdk;

 sdk: ">=2.12.0 <3.0.0" 

现在我必须通过声明来更改所有变量?在所有字符串和整数上。

但是我该怎么处理表单键呢?

 if (_formKey.currentState.validate())

这是 Android Studio 告诉我的,但我不确定该放在哪里?

The method 'validate' can't be unconditionally invoked because the receiver can be 'null'.  Try making the call conditional (using '?.') or adding a null check to the target ('!').

知道我应该如何将此行转换为空安全吗?

这个怎么样?

 Future<File> file;

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    这是第一个;

    (_formKey.currentState!.validate())
    

    还有下一个;

     late Future<File> file;
    

    【讨论】:

    • 这可能对您的特定代码有效,但这不是一般规则。对于其他人,在您的程序之外的另一个上下文中使用确切的 sn-p 代码,这可能是错误的。
    猜你喜欢
    • 2021-08-09
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2021-11-22
    • 2021-04-21
    • 2021-08-12
    相关资源
    最近更新 更多