簡體   English   中英

關閉之前 Flutter 項目的 Null 安全?

[英]Turn off Null Safety for previous Flutter Project?

我想升級我的 flutter 以獲得 null 安全等新功能,但我不希望我以前的項目影響它們。 我只想對我的新 flutter 項目進行新的更改,我想以類似於舊方式的方式運行我的舊項目。 有什么辦法嗎? 請指導我完成它。

謝謝你

在舊項目的pubspec.yaml文件中設置 SDK 約束就足夠了。

例如,以下未啟用 null 安全:

environment:
  sdk: ">=2.11.0 <3.0.0"

您還可以在 Dart 文件的頂部指定以禁用對該文件的 null 檢查。

// @dart=2.9

在我在beta通道上升級到Dart v2.12后,上述答案對我不起作用。 所以我找到了這些選項:

您可以將此添加到任何 dart 文件的頂部以禁用空安全檢查。

// @dart=2.9

或類似於上面的答案,我必須包含 2.12 之前的版本來禁用 null 安全。 您將在pubspec.yaml文件中編輯此行。

environment:
  sdk: ">=2.11.0 <3.0.0"

您可以在沒有 null 安全性的情況下運行 flutter 項目,使用--no-sound-null-safety選項和flutter run

您也可以在launch.json的 launch.json 中將此作為參數添加

"configurations": [
        {
            "name": "Flutter",
            "request": "launch",
            "type": "dart",
            "flutterMode": "debug",
            "args": [
                "--no-sound-null-safety"
            ],
        },
]

您的項目可能是使用 flutter 舊版本(版本低於 2.+)開發的。 2.xx dart 版本的主要變化是啟用 null 安全性。 目前,pub.dev 上的很多庫已經升級到 null 安全特性。

但是您的舊項目可能有一些庫仍未更新到 null 安全性。 因此,您的項目可能兩者兼而有之。 在這種情況下,@miguel 的答案部分有效(定義sdk: ">=2.7.0 <3.0.0"約束)。 要運行您的項目,您還需要取消零安全性。 就像通過運行以下命令

flutter 運行 --no-sound-null-safety

或通過 go 在配置中添加此命令到Run->Edit Configurations 它將打開以下彈出窗口,突出顯示的字符串與在此處輸入圖像描述

**推薦:**將您的項目更新為 null 安全。 閱讀有關零安全性的更多信息

I had the same problem and after doing a lot of downgrading and upgrading (especially when the old project needed to be built with the old version of Flutter and build_runner) I found out about Flutter Version Manager check the git repo here: https:// github.com/leoafarias/fvm 最好的一點是您可以指定每個項目要使用的版本。

以下來自回購中的說明:

  1. 激活全局運行pub global activate fvm
  2. 要安裝 Flutter 的特定版本,請運行fvm install <version>
  3. 然后 go 進入項目根目錄並運行fvm use <version>

等等。 希望能幫助到你 。

檢查 repo 以獲取更多命令,例如fvm use <version> --global來輕松切換全局版本和更有趣的東西。

我有同樣的問題。

升級到 Flutter 2.0 后需要重新獲取包

通過運行執行此操作:

flutter pub get

或者通過將pubspec.yaml文件保存在 VS 代碼中,這將為您獲取包。

您可以使用 var 或 dynamic 聲明變量,現在編譯器不會檢查variable type

var answerText;
dynamic answerColor;

Answer({this.answerText, this.answerColor});

如果您嘗試在沒有 null 安全檢查的情況下進行構建,請在 main.dart 第一行中使用此注釋行// @dart=2.9

// @dart=2.9

import 'package:flutter/material.dart';

void main() async {
  
  runApp(
    const MyApp()
  );
}

Delete the following line of code from gradle file or just comment it out to run the code without the Gradle Exeception- if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local .properties 文件。") }

如何在 xcode 14 中應用 --no-sound-null-safety 配置?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM