Flutter-开发常用笔记

代理设置

修改系统的环境变量,增加HTTP_PROXY、HTTPS_PROXY和NO_PROXY变量,例如:

HTTP_PROXYhttp://127.0.0.1:10809
HTTPS_PROXYhttps://127.0.0.1:10809
NO_PROXYlocalhost,127.0.0.1,::1

Android license status unknown

在执行flutter doctor后,有一条错误信息是“Android license status unknown”。

这时需要执行flutter doctor --android-licenses,顺利的话,一路yes就可以了。

Android sdkmanager tool not found

如果AndroidStudio里下载失败,可以手动下载sdkmanager。去https://developer.android.com/studio/index.html#downloads里,寻找最新的“Command line tools only”,如图。

下载后,解压至Android\Sdk\cmdline-tools\latest目录下。Android\Sdk位置可在AndroidStudio设置里找到,如图:

这时运行一些命令可能还会提示“A newer version of the Android SDK is required. ”这时,可通过AndroidStudio的SDK Manager来更新,如图,在“SDK Tools”选项卡中,点击“Android SDK Tools”项改为对号,然后“Apply”即可安装更新。

设置ANDROID_HOME

在适配Android系统时,有时会报找不到ANDROID_HOME的错误,所以提前设置好这个环境变量准没错。

ANDROID_HOME的具体路径可在SDK Manager里查看,如图。

在“环境变量->用户变量”里新建ANDROID_HOME,并将具体路径复制进去。

然后在Path变量中,添加%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools

长时间Running “flutter pub get”…

或者类似“Downloading the Dart SDK using the BITS service failed, retrying with WebRequest”的错误。

即使翻墙了,有时也会遇到这种问题。解决方案是添加两条环境变量,原理是更换服务器。

PUB_HOSTED_URL=https://mirrors.tuna.tsinghua.edu.cn/dart-pub

FLUTTER_STORAGE_BASE_URL=https://mirrors.tuna.tsinghua.edu.cn/flutter

或者:

PUB_HOSTED_URL=https://pub.flutter-io.cn

FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

Unable to load asset

在加载本地图片资源时,出现了如下错误。

The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/1.jpg

然后我再三检查了路径“assets/images/1.jpg”是正确的。最终查到了原因,是pubspec.yaml文件里的路径格式写错了。如图,assets前应该有两个空格,而- assets/images/前应该有4个空格。

Unhandled Exception: MissingPluginException

当安装新插件后,没有停止app后重新运行,而是习惯性以热加载的方式运行新代码时,就会出现这个异常。

解决方案自然是停止app后重新运行。

No permissions found in manifest

使用permission_handler插件做权限管理,在请求camera权限时,出现了“No permissions found in manifest”字样的错误,但我明明在AndroidManifest.xml文件里添加了<uses-permission android:name="android.permission.CAMERA"/>啊。

后来发现,我只是在profile文件夹下的AndroidManifest.xml文件里添加了……所以在debug模式中是无效的。所以应该在debug或main文件夹下的AndroidManifest.xml文件里添加需要动态请求的权限。

AnimatedWidget与AnimatedBuilder

这俩区别……呃……感觉不大。直接看示例代码吧,对比一下。

class AnimatedLogo extends AnimatedWidget{
  AnimatedLogo(Animation<double> animation, {this.child}):super(listenable:animation);

  Widget child;

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;

    return Container(
      width: animation.value,
      height: animation.value,
      child: child,
    );
  }
}

class AnimatedLogo2 extends StatelessWidget{
  AnimatedLogo2(this.animation, this.child);

  Animation<double> animation;
  Widget child;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animation,
      builder: (animatedContext, animatedChild){
        return Container(
          width: animation.value,
          height: animation.value,
          child: child,
        );
      },
    );
  }
}

Waiting for another flutter command to release the startup lock

在连续执行多个flutter命令时,有时会出现这个提示,关闭IDE都不行,只能重启电脑。其实打开任务管理器,关闭所有dart进程即可。

控件填充整个区域

Container(constraints: BoxConstraints.expand())

创建本地插件

例如新建本地插件baiduasr_plugin,完成后删除其自动创建的sample文件夹。

pubspec.yaml中引入该插件。

另外,在安卓(java)工程下,遇到找不到flutter符号的问题,需要手动添加flutter.jar依赖。如果引入第三方jar库,还需要添加libs文件夹依赖。参考设置如图。

清理Flutter工程文件

当我们需要分享工程代码时,就要删除一些临时文件,仅保留必须的文件,以减少体积。

在Android Studio里,点击菜单“Tools->Flutter->Flutter Clean”即可。

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注