Flutter 开发出现的那些 Bugs 和解决方案「持续更新... 」

4,860 阅读2分钟

记录 Flutter 开发过程中遇到的一些问题和相关的解决方案~

1. --no-sound-null-safety 错误 on vscode

上面是 VSCode 编辑器中空校验错误。解决方案如下:

// vscode 编辑器项目根目录中创建文件 .vscode/launch.json
// 添加内容
"args": [
  "--no-sound-null-safety"
]

// 完整的文件代码示例
{
  "configurations":[
    {
      "name": "jimmy flutter demo",
      "program": "lib/main.dart",
      "request": "launch",
      "type": "dart",
      "args": [
        "--no-sound-null-safety"
      ]
    }
  ]
}

// 之后重新运行项目即可

2. 设置 flutter_screenutil 报错

直接引用包 flutter_screenutil 去使用,会报错使用不了 ScreenUtil().setWidth(width) 等方法。

这个错误就是我们并没有按照官网进行使用。需要根据官网逐步进行。

这里我使用方式一 -- 在app中使用它一次。

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //填入设计稿中设备的屏幕尺寸,单位dp
    return ScreenUtilInit(
      designSize: const Size(360, 690),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context , child) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'First Method',
          // You can use the library anywhere in the app even in theme
          theme: ThemeData(
            primarySwatch: Colors.blue,
            textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp),
          ),
          home: child,
        );
      },
      child: const HomePage(title: 'First Method'),
    );
  }
}

3. 构建安卓应用包出错

在应用开发完成之后,构建安卓的图标文件,可能会出现报错:

✗ ERROR: InvalidConfigException 
Cannot not find minSdk from android/app/build.gradle or android/local.propertiesSpecify minSdk in either android/app/build.gradle or android/local.properties
#0      createIconsFromConfig (package:flutter_launcher_icons/main.dart:96:7)
#1      createIconsFromArguments (package:flutter_launcher_icons/main.dart:60:7)
#2      main (file:///Users/jimmy/Documents/sdk/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_launcher_icons-0.9.3/bin/main.dart:7:26)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

可以在 android/local.properties 文件中添加内容:

flutter.versionName=1.0.0
flutter.versionCode=1
flutter.minSdkVersion=30

在应用开发完成之后,针对安卓应用去打包,可能会出现报错:

Execution failed for task ':app:processReleaseMainManifest'. Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 21 declared in library

这是因为 minSdkVersion 的问题,我们更改下相关的文件版本即可:

// 解决方案
// android/app/build.gradle

defaultConfig {
        // // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        // applicationId "com.example.jimmy_flutter_demo"
        // // You can update the following values to match your application needs.
        // // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        // minSdkVersion flutter.minSdkVersion
        // targetSdkVersion flutter.targetSdkVersion
        // versionCode flutterVersionCode.toInteger()
        // versionName flutterVersionName
        
        applicationId "com.example.jimmy_flutter_demo"
        minSdkVersion 21 // 更改最小的版本
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

如果构建成功,你会看到输出目录提示内容类似下面:

✓  Built build/app/outputs/flutter-apk/app-armeabi-v7a-release.apk (7.5MB).

How is HTTP Host Availability in flutter 2.10?

运行 flutter doctor -v 发生类似的错误:

[!] HTTP Host Availability
    ✗ HTTP host https://maven.google.com/ is not reachable. Reason: An error
      occurred while checking the HTTP host: Operation timed out

解决方案请戳 这里,或者看下面:

1.  Open ${FLUTTER_HOME}\packages\flutter_tools\lib\src\http_host_validator.dart
1.  Change `https://maven.google.com/` to `https://dl.google.com/dl/android/maven2/`
1.  Close Terminal
1.  Open ${FLUTTER_HOME}\bin and delete cache directory
1.  Reopen Terminal and exec `flutter doctor`

这个问题无关紧要,解决不解决都行 - Flutter_HTTP Host Availability

目前暂时遇到这些 Bug,后续会持续更新... 欢迎关注收藏

如果读者觉得文章还可以,不防一键三连:关注➕点赞➕收藏