如何在Flutter中启用Null-Safety? [英] How to enable Null-Safety in Flutter?

查看:732
本文介绍了如何在Flutter中启用Null-Safety?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用null安全性,但这给了我这个错误:

I tried to use null safety, but it's giving me this error:

这需要启用不可为空"的语言功能.尝试更新您的pubspec.yaml以将最低SDK约束设置为2.10.0或更高版本,并运行"pub get".

This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.10.0 or higher, and running 'pub get'.

我将Dart SDK约束从 2.7.0 更改为 2.10.0 ,但仍显示此错误.

I changed my Dart SDK constraint from 2.7.0 to 2.10.0, but it's still showing this error.

我还升级了Dart and Flutter SDK:

Also, I upgraded my Dart and Flutter SDK:

dart-sdk v2.10.2是基于您的最新可用版本来源.

dart-sdk v2.10.2 is the latest version available based on your source(s).

Flutter(频道稳定,1.22.3,...

Flutter (Channel stable, 1.22.3, ...

推荐答案

从Dart 2.12 开始,不再安全是一项实验 .现在很容易启用.

Null safety is no longer an experiment as of Dart 2.12. It is now easy to enable.

从第一个Dart 2.12 版本开始,类型将默认为不可为空.因此,您只需要更改您的SDK约束即可:

Starting with the first Dart 2.12 versions, types will be non-nullable by default. So you just need to change your SDK constraint:

environment:
  sdk: ">=2.12.0 <3.0.0"


详细了解启用空安全性";在dart.dev 上.

这篇关于如何在Flutter中启用Null-Safety?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆