Flutter 2.0 踩坑记录

flutter版本:2.0.5
有时间就写,慢慢填坑

参考

flutter.cn

flutter

开发工具

一些功能可以提高开发效率,建议一看

Android Studio
vs code

常用命令

flutter clean #清空构建和依赖缓存
flutter pub get #拉取依赖

空安全支持

迁移指南
要确保你使用的库也支持空安全

# 自动升级
dart migrate --apply-changes

平台判断

在web浏览器的环境下不能使用Platform判断,判断是否为web用kIsWeb

import 'dart:io';

import 'package:flutter/foundation.dart';

class DeviceType {
  static bool isWeb = kIsWeb;

  static bool isIOS = !isWeb && Platform.isIOS;
  static bool isAndroid = !isWeb && Platform.isAndroid;
  static bool isMacOS = !isWeb && Platform.isMacOS;
  static bool isLinux = !isWeb && Platform.isLinux;
  static bool isWindows = !isWeb && Platform.isWindows;

  static bool get isDesktop => isWindows || isMacOS || isLinux;

  static bool get isMobile => isAndroid || isIOS;

  static bool get isDesktopOrWeb => isDesktop || isWeb;

  static bool get isMobileOrWeb => isMobile || isWeb;
}

Adnroid打包

# 为arm、arm64生成不同的apk,减小apk大小
flutter build apk --target-platform android-arm,android-arm64 --split-per-abi
# 安装
adb install .\build\app\outputs\flutter-apk\app-arm64-v8a-release.apk

flutter plugin

ios

ios在编写原生的代码的时候,要先在Android studio运行一下,让CocoaPods生成一下项目文件,然后再用xcode打开,否则在目录都看不到原生的plugin文件

Web

启用web支持

flutter config --enable-web
## 在已有项目的根目录创建web模板
flutter create .

运行

flutter run -d chrome

打包

web打包可以选择两种渲染模式,pc端对应canvaskit,移动端对应html

# 生成文件在build/web
# 移动端优化
flutter build web --web-renderer html
# pc端优化
flutter build web --web-renderer canvaskit

起服务

需要python环境支持

python -m http.server 80 --directory .\build\web\

Windows

启用windows支持

flutter config --enable-windows-desktop
## 在已有项目的根目录创建windows模板
flutter create .

运行

flutter run -d windows

打包

# 生成文件在build/windows/runner/Release
flutter build windows

打包成安装程序工具MSIX

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值