Flutter笔记———小tips

245 阅读3分钟

Flutter相关

结构

文件夹作用
common一些工具类,如通用方法类、网络接口类、保存全局变量的静态类等
l10n国际化相关的类都在此目录下
modelsJson文件对应的Dart Model类会在此目录下
states保存APP中需要跨组件共享的状态类
routes存放所有路由页面类
widgetsAPP内封装的一些Widget组件都在该目录下

全局变量和共享状态

应用程序中通常会包含一些贯穿APP生命周期的变量信息,这些信息在APP大多数地方可能都会被用到,比如当前用户信息、Local信息等。在Flutter中我们把需要全局共享的信息分为两类:全局变量和共享状态。全局变量就是单纯指会贯穿整个APP生命周期的变量,用于单纯的保存一些信息,或者封装一些全局工具和方法的对象。而共享状态则是指哪些需要跨组件或跨路由共享的信息,这些信息通常也是全局变量,而共享状态和全局变量的不同在于前者发生改变时需要通知所有使用该状态的组件,而后者不需要。为此,我们将全局变量和共享状态分开单独管理。

报错

Error (Xcode): ../../../.pub-cache/hosted/pub.flutter-io.cn/file-6.1.2/lib/src/interface/file.dart:15:16: Error: The method 'File.create' has fewer named arguments than those of overridden method 'File.create'.

file版本比较低, 需要升级, 执行完之后升级到了 6.1.4,
解决方案:

pub upgrade
pub get

作者:malgee
链接:www.jianshu.com/p/f621901e2…
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

安卓相关

SDK

SDK 软件开发工具包 安卓开发先安装JDK 后安装Androidstudio 下载Android SDK及配置模拟器

打包

  • 在 VSCode 中的 终端中输入 flutter build apk,如果最终显示 ✓ Built ***,说明打包成功

  • build/app/outputs/flutter-apk/app-release.apk是在项目中的子路径,到这里,APK 打包就已经顺利成功。

iOS相关

iOS端打包上线流程*

  1. flutter clean
  2. flutter build ios --release
  3. 打开Xcode  Product ——>build
  4. Product ——>archive

上传即可

报错 The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods install*

解决:删除以下文件:

xcworkspace

Podfile.lock

Pods文件夹

~/Library/Developer/Xcode/DerivedData路径下对应工程的文件夹

之后重新执行pod install --verbose --no-repo-update


Xcode不更新即可运行高版本真机进行测试***

iOSDeviceSupport


存放路径:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

Xcode 模拟器安装路径*

~/Library/Developer/CoreSimulator/Profiles/Runtimes/

flutter 引入fluwx报错The number of method references in a .dex file cannot exceed 64K*

针对 MultiDex 配置应用

minSdkVersion 设为 21 或更高版本,系统会默认启用 MultiDex,

配置环境变量

touch ~/.bash_profile
open ~/.bash_profile
export PATH=~/code/flutter_dir/flutter/bin:$PATH
source $HOME/.bash_profile

如果你使用终端是zsh,终端启动时 `~/.bash_profile` 将不会被加载,解决办法就是修改 `~/.zshrc` ,在其中添加:source ~/.bash_profile

安装Cocopods

安装Xcode

xcode-select --install

gem sources -l
gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/
sudo gem install -n /usr/local/bin cocoapods
pod setup

第二种方式
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install cocoapods
pod setup

web相关

跨域问题

  1. 安装shelf_proxy依赖库
dependencies:
  shelf_proxy: ^1.0.1
  1. lib下新建proxy.dart
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_proxy/shelf_proxy.dart';

Future<void> main() async {
  final server = await shelf_io.serve(
    proxyHandler('https://dart.cn'),
    'localhost',
    8080,
  );

  print('Proxying at http://${server.address.host}:${server.port}');
}
  1. 运行服务
dart ./lib/proxy.dart
  1. 将项目中的接口地址改成localhost:8080

手机调试

如果想用手机上的浏览器调试项目,需要本地部署一下:

flutter run -d chrome --web-hostname 192.168.3.156 --web-port 8080

ip地址换成你电脑的ip地址即可,然后手机和电脑在同一网络环境下,就可以输入上面的ip+端口号访问web项目了

桌面

需要在macos/Runner/DebugProfile.entitlements文件中添加:

<key>com.apple.security.network.client</key>`
<true/>