2022年4月

几条常用命令

flutter build appbundle    #打包 App Bundle,发布 Google Play 时必须用的。
flutter build apk --release    #打包 APK
flutter build ios    #生成IOS资源

安装后检查环境:

flutter doctor

如果出现:
[!] Android Studio (not installed)
一条指令:flutter config --android-studio-dir="D:\tools\Android Studio"

查看当前设置使用命令 flutter config
禁用崩溃报告:flutter config --no-analytics

默认ADB目录(可以加入path):
C:\Users\Admin\AppData\Local\Android\Sdk\platform-tools

手动安装APK:
cd C:\Users\Admin\AppData\Local\Android\Sdk\tools
将apk复制到此目录
adb install app-release.apk

打开虚拟设备时,弹出“unable to locate adb”
File -> Project Structure -> Project, 选择 SDK

项目不能运行(调试按钮灰色):
下载Dart SDK: https://dart.dev/tools/sdk/archive
解压缩,并到 Setting - Languages & Frameworks - Dart 中,设定 Dart SDK path,并在下方勾选项目开启Dart支持;
Setting - Languages & Frameworks - Flutter 中,指定 Flutter SDK path;

解决控制台中文输出乱码:
双击Shift,输入vmoption,,选择Edit Custom CM Options,如果没有配置过,选择 Create,增加一行:
-Dfile.encoding=UTF-8
[这个网上找的,对我自己不生效,windows中文一直乱码,MacOS不会]

flutter doctor, 出现 cmdline-tools component is missing
Setting - Appearance & Behavior - System Setting - Android SDK - SDK Tools, 确保有勾选:

  • Android SDK Build-Tools 31
  • Android SDK Command-line Tools (latest)
  • Android SDK Platform-Tools
  • Android SDK Tools (Obsolete)

至Android SDK 目录/Tools/bin/, sdkmanager --install "cmdline-tools;latest"
添加到path:
%ANDROID_HOME%\cmdline-tools\latest\bin
%ANDROID_HOME%\platform-tools
注意环境变量不可少:ANDROID_HOME, ANDROID_SDK_ROOT, JAVA_HOME,前2个都配置到 Android SDK 目录

Flutter APP 旧版本怎么更新到新版本
最好的办法是直接新建项目,再从旧版本中复制所有你需要的文件,主要是 assets 目录、lib 目录、pubsec.yaml,然后重新运行。

出错:ANDROID_SDK_HOME was meant to be the parent path of the preference folder expected by the Android tools.
It is now deprecated.
删除环境变量中的 ANDROID_SDK_HOME

Gradle 下载慢的问题:
project/android/gradle/wrapper/
删除 gradle_wrapper.jar
修改 gradle-wrap.properties,distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.2-bin.zip

修改中国源,增加系统变量:

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

command not found: flutter
open ~/.zshrc
open ~/.bash_profile
都加上(注意改 Flutter 路径):
export PATH="$PATH:/Users/xiong/Documents/flutter/bin"
然后 source ~/.zshrc

治疗各种 Pod 包问题:
删除 ios 目录下的 Podfile.lock
cd ios
pod install --repo-update

到了2022年,闲鱼框架已经差不多要退出历史舞台了,但它给Flutter还是作出了巨大的贡献的,特别是中国的Flutter开发者。
这篇文章只是记录一下方便自己查阅,对于新学或者是新建项目者,还是应该考虑当前最主流的 GetX 和 Provider。

闲鱼文件结构
page:总页面,注册effect,reducer,component,adapter的功能,相关的配置都在此页面操作
state:这地方就是我们存放子模块变量的地方;初始化变量和接受上个页面参数,也在此处,是个很重要的模块
view:主要是我们写页面的模块
action:这是一个非常重要的模块,所有的事件都在此处定义和中转
effect:相关的业务逻辑,网络请求等等的“副作用”操作,都可以写在该模块
reducer:该模块主要是用来更新数据的,也可以写一些简单的逻辑或者和数据有关的逻辑操作

几句话搞懂他们的关系:
page 入口,所有功能在这初始化
state 可以理解为一个结构类,存放所有变量的,在各个页面之间共享
action 是一个枚举类,定义 action 的集合,当发起 action 的时候,根据 action 名字调用 reducer
reducer 处理发起的 action,并更新 state
view 负责视图层的构建,由 state 驱动
effect 处理所有副作用的函数,不做任何数据处理。分为两种,一种是对生命周期的回调,一种是对非处理数据事件的回调

错误内容:

提示:找不到导入的项目“C:\Program Files\Microsoft Visual
Studio\2022\Community\MSBuild\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets”。此外,已尝试在
$(MSBuildExtensionsPath32) - "C:\Program Files (x86)\MSBuild"
的回退搜索路径中找到“Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets”。这些搜索路径在“C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\17.0_fa40166f\devenv.exe.config”中定义。确认
声明中的路径正确,并且该文件位于某个搜索路径中的磁盘上。

解决办法

修改 .csproj:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" />

将路径中的 v16.0 改成:v$(VisualStudioVersion)

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />

建类:ViewBagActionFilter

namespace MetaWeb.Core
{
    public class ViewBagActionFilter : ActionFilterAttribute
    {

        public ViewBagActionFilter()
        {
            //DI will inject what you need here
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            // for razor pages
            if (context.Controller is PageModel)
            {
                var controller = context.Controller as PageModel;
                controller?.ViewData.Add("Langs", Langs.instance());
                //also you have access to the httpcontext & route in controller.HttpContext & controller.RouteData
            }

            // for Razor Views
            if (context.Controller is Controller)
            {
                var controller = context.Controller as Controller;
                if(controller != null)
                    controller.ViewBag.Langs = Langs.instance();
            }

            base.OnResultExecuting(context);
        }
    }
}

Program.cs 中添加:

builder.Services.AddControllersWithViews(options => {
    options.Filters.Add<ViewBagActionFilter>();
});

新建处理类:

public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
    public string? TransformOutbound(object? value)
    {
        if(value == null) return null;
        string? result = value.ToString();
        if(result == null) return null;

        return Regex.Replace(result, "([a-z])([A-Z])", "$1-$2").ToLower();
    }
}

路由注入 Program.cs :

builder.Services.AddRouting(options => {
    options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
    options.LowercaseUrls = true;
});

路由:

app.MapControllerRoute(name: "default", pattern: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");