windows下flutter的环境安装

Flutter是谷歌出品的移动应用SDK,性能卓越、体验精美、跨平台、HotReload等等这些特点。

Dart是谷歌推出的编程语言。支持即时编译JIT(Just In Time)、HotReload(热加载)和事前编译AOT(Ahead of Time)。

所以搜索语法要用dart。

一、下载

二、环境变量

复制代码
PUB_HOSTED_URL=https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

注意:这两个环境变量一定要加,否则可能导致后面 flutter doctor 命令连接不上服务

三、安装

安装git

安装android studio

会让你下载SDK,记一下路径,然后配置到环境变量中。

复制代码
ANDROID_HOME = Android sdk路径

如果忘记了或者没记或者跳过了安装安卓SDK的步骤:

安装Flutter SDK

把之前下载的SDK直接解压。

比如我的是:C:\sdk\flutter

然后把bin目录配置到环境变量中:

复制代码
C:\sdk\flutter\bin

打开cmd检测下:

复制代码
flutter --version 查看是否安装成功
flutter -h  能够展示指令,表示安装配置成功

四、flutter doctor 环境检测

在前面都安装完后,才是正式安装的开始。

打开cmd,运行 flutter doctor

这是运行成功的例子,那么哪个不对改哪里。

4.1 安卓SDK下载不了

AppData\\Roaming\\Google 下缓存删掉

4.2 HTTP Host availability check is taking a long time...

  1. 找到flutter sdk的文件目录,依次找到flutter/packages/flutter_tools/lib/src/http_host_validator.dart文件
  2. https://maven.google.com/ 修改为https://dl.google.com/dl/android/maven2/
  3. 关闭所有打开了flutterSDK的程序,找到flutter\bin目录下的flutter_tools.snapshot文件,删除。
  4. 重新打开cmd,再试。(注意要重新打开)

4.3 Windows Version (Unable to confirm if installed Windows version is 10 or greater)

  1. 打开:\packages\flutter_tools\lib\src\windows\windows_version_validator.dart
  2. 直接替换:
dart 复制代码
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:process/process.dart';

import '../base/io.dart';
import '../doctor_validator.dart';

// FIX #1 - Remove everything from line 10 to 20 in original source code.

/// Validator for supported Windows host machine operating system version.
class WindowsVersionValidator extends DoctorValidator {
  const WindowsVersionValidator({required ProcessManager processManager})
      : _processManager = processManager,
        super('Windows Version');

  final ProcessManager _processManager;

  @override
  Future<ValidationResult> validate() async {

// FIX #2 - Replace 'systeminfo' by 'ver' command
    final ProcessResult result =
        await _processManager.run(<String>['ver'], runInShell: true);

    if (result.exitCode != 0) {
      return const ValidationResult(
        ValidationType.missing,
        <ValidationMessage>[],
        statusInfo: 'Exit status from running `systeminfo` was unsuccessful',
      );
    }

    final String resultStdout = result.stdout as String;

// FIX #3 - Remove brackets from output
    final String resultAdjusted = resultStdout.replaceAll('[','').replaceAll(']','');

// FIX #4 - Split the output at spaces, and get Windows version at position 3.
//          Split again at dots and get the major version at position 0.
//          Cast the output to int.
    final int winver = int.parse(resultAdjusted.split(' ').elementAt(3).split('.').elementAt(0));

    // Use the string split method to extract the major version
    // and check against the [kUnsupportedVersions] list
    final ValidationType windowsVersionStatus;
    final String statusInfo;

// FIX #5 - Check if Windows major version is greater than 10.
//          Succeeds if true.
    if (winver >= 10) {
      windowsVersionStatus = ValidationType.installed;
      statusInfo = 'Installed version of Windows is version 10 or higher';
    } else {
      windowsVersionStatus = ValidationType.missing;
      statusInfo =
          'Unable to confirm if installed Windows version is 10 or greater';
    }

    return ValidationResult(
      windowsVersionStatus,
      const <ValidationMessage>[],
      statusInfo: statusInfo,
    );
  }
}
  1. 删除文件:\bin\cache\flutter_tools.stamp
  2. 重新打开cmd,再试。(注意要重新打开)

4.4 如果不在PC上运行,Visual Studio 不用管。

4.5 android-licenses

这个是需要同意一下协议。

复制代码
flutter doctor --android-licenses
然后全部同意就行了。

五、使用

5.1 找不到dart sdk

复制代码
C:\\sdk\\flutter\\bin\\cache\\dart-sdk

5.2 卡在Running Gradle task 'assembleDebug'

属于网络问题,国内对一些镜像是有墙的,换一下就行。你自己翻也行。

  1. 打开flutter\packages\flutter_tools\gradle\flutter.gradle

  2. 改:

    private static final String DEFAULT_MAVEN_HOST = "https://storage.googleapis.com";

    改成:

    "https://storage.flutter-io.cn";

dart 复制代码
buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { allowInsecureProtocol = true
            url 'http://maven.aliyun.com/nexus/content/groups/public' }
        //google()
        //mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { allowInsecureProtocol = true
            url 'http://maven.aliyun.com/nexus/content/groups/public' }
        //google()
        //mavenCentral()
    }
}
  1. 重新编译运行即可。

项目文件中android/build.gradle也有相关配置,改一下也可以,即只对项目生效。

5.3 Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven...

这是因为上一个问题中:allowInsecureProtocol = true没有加,或者路径没有加https。

六、总结

Flutter使用的是谷歌生态,所以有很多网络不通的情况,如果编译卡住了,可以首先怀疑是网络墙了。

这是我个人安装过程中的一些问题,如果还有其他问题,欢迎评论补充。如果有不对的,或者试了没有效果的,也欢迎指正。

相关推荐
阿郎_20112 小时前
window10的wsl安装配置ubuntu22.04和docker
运维·windows·ubuntu·docker·容器
徐安安ye2 小时前
Flutter 与 Rust 混合开发:打造毫秒级响应的高性能计算引擎
开发语言·flutter·rust
xianjixiance_11 小时前
Flutter跨平台三方库鸿蒙化适配指南
flutter·华为·harmonyos
junlaii11 小时前
Windows 安装 claude code 教程
windows·ai
SoaringHeart13 小时前
Flutter组件封装:视频播放组件全局封装
前端·flutter
非凡ghost14 小时前
CoolUtils PDF Combine(PDF合并工具)
windows·学习·pdf·软件需求
就叫飞六吧15 小时前
JSONPath“隔空取物”思想,直击JSON深处的目标字段
服务器·windows·json
love530love15 小时前
【笔记】ComfyUI “OSError: [WinError 38] 已到文件结尾” 报错解决方案
人工智能·windows·python·aigc·comfyui·winerror 38
程序员老刘17 小时前
Kotlin vs Dart:当“优雅”变成心智负担,我选择了更简单的 Dart
flutter·kotlin·dart
徐安安ye17 小时前
Flutter 车载系统开发:打造符合 Automotive Grade Linux 标准的 HMI 应用
linux·flutter·车载系统