Flutter 报错问题小结

前言

本篇主要记录一下在学习Flutter的过程中遇到的一些问题,及解决方法,菜鸟学习,持续更新中...

1. Flutter添加图片的踩坑过程

今天添加图片一直报错:

Error on line 48, column 4: Expected a key while parsing a block mapping.
   ╷
48 │    assets:
   │    ^
   ╵

这是因为图片文件夹目录路径不对,要放在项目目录也就是Lib文件夹下:

image.png

在Flutter中,所有的图片资源与自定义字体资源,默认情况下都需要添加在Flutter应用根目录下的assets目录下(assets文件目录需要用户自己新建,名称必须是“assets”,否则会报找不到图片的错误),并且需要在项目的pubspec.yaml文件下对应的位置的添加图片名称。

然后放开 pubspec.yaml 中 assets 里的注释,内容改成如图所示的,但是依然报错,这是因为配置文件,pubspec.yaml 中未对齐,层次不对,注意“-”前后的空格,Flutter对空格有很严格的要求,

image.png

对齐以后就好了:

image.png

码代码,使用 Card 组件添加一个图片

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        // 应用顶栏
        appBar: AppBar(
          title: Text("Hello World"),
        ),
        body: Card(
          child: Column(
            children: [Image.asset('asset/images/lake.png'), Text('添加图片')],
          ),
        ),
      ),
    );
  }
}

但是依旧报错,读不到这个图片,

image.png

pubspec.yaml 配置文件中拼接上全路径就可以了:

assets:
    - assets/images/lake.png

把Assets 文件夹移出来和lib平级,pubspec.yaml 中写不写图片名字都可以。

image.png

2. 运行报错问题如下:

终端中的报错信息:

The Swift pod amap_special depends upon MJExtension, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers! globally in your Podfile, or specify :modular_headers => true for particular dependencies.

The Swift pod barcode_scan depends upon MTBBarcodeScanner, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers! globally in your Podfile, or specify :modular_headers => true for particular dependencies.

image.png

Android Studio 中的报错信息:
Command CompileSwift failed with a nonzero exit code

:0: error: could not build Objective-C module 'amap_special'

image.png

一堆这个报错:

image.png

:0: error: could not build Objective-C module 'amap_special' note: Using new build system note: Building targets in parallel note: Planning build note: Using build description from disk

image.png

在Flutter项目中导入了二维码扫描包 和 amap_special 地图

  # 扫码
  barcode_scan: ^3.0.1

然后在Android端和web是没有问题的,但是在iOS运行就会报上面的错误,既然是外接包方面的那就是pods上面的问题了,网上百度了很多,但是没能解决问题,也有讲OC项目和Swift语言版本的问题,后来也一直以为是语言的问题呢,还有pod clean 再install的,更改Build Setting设置的,各种方式,尝试了一遍也没解决,

我看网上还有说下面barcode_scan这个报错的,在profile 文件中添加 use_frameworks!然后pod install,是的,这样可以解决这个问题,但是地图的依然报错:

image.png

直到今天下午再一位热心同事的帮助下才解决了问题,说到底其实很简单,下面的报错信息已经提示的很明白了,奈何不好好的仔细研究,混乱一通的百度,事倍功无。

不想看上面踩坑历程的,直接看这里:

解决方法:

只需要这样:

#use_frameworks!
use_modular_headers!

明白了吧?So easy!运行OK,折磨半天的问题终于解决了,特此记录一下,给踩坑的小伙伴做个参考。

3. 引用库工程,库工程引入第三方插件,插件中引入静态库

use_frameworks!报错找不到文件,改用use_modular_headers!,直接崩溃,控制台报错如下:

Library not loaded: @rpath/libswiftCore.dylib
Referenced from: /private/var/containers/Bundle/Application/6269DEE5-8E97-4422-814D-25079C5B196E/Runner.app/Runner
Reason: tried: '/usr/lib/system/introspection/libswiftCore.dylib' (no such file), '/private/var/containers/Bundle/Application/6269DEE5-8E97-4422-814D-25079C5B196E/Runner.app/Frameworks/libswiftCore.dylib' (no such file), '/private/var/containers/Bundle/Application/6269DEE5-8E97-4422-814D-25079C5B196E/Runner.app/Frameworks/libswiftCore.dylib' (no such file), '/usr/local/lib/libswiftCore.dylib' (no such file), '/usr/lib/libswiftCore.dylib' (no such file)
(lldb)

在这里添加

/usr/bin/swift
/usr/lib/swift

image.png

运行OK,解决了

4. Flutter引用jpush_flutter: 2.4.0第三方插件,iOS运行pod install 报错,如下:

[!] CocoaPods could not find compatible versions for pod "JCore": In Podfile: janalytics (from.symlinks/plugins/janalytics/ios`) was resolved to 0.0.1, which depends on
JCore (= 2.6.2)

jpush_flutter (from.symlinks/plugins/jpush_flutter/ios`) was resolved to 0.0.2, which depends on
  JCore (= 3.2.5)

`


image.png

这是因为Flutter引用了极光的两个插件 janalytics: 2.1.5和jpush_flutter: 2.4.0,引用的JCore版本不一致导致的。

解决:

将jpush_flutter的JCore模块降级版本:

点开ios目录下.symlinks/plugins,打开jpush_flutter/ios/jpush_flutter.podspec文件,修改如下两行:

image.png

改为

s.version = '0.0.1'
s.dependency 'JCore','2.6.2'

5. Could not build the precompiled application for the device.

Error (Xcode): Cycle inside ReachabilitySwift; building could produce unreliable results.

解决方法

今天一直报各种错误,这个错误发现每次运行Xcode都需要clean才行,不彻底,清除缓存,重启,然后在flutter项目目录下执行 flutter clean 命令,重启,重新运行。

6.When a row is in a parent that does not provide a finite width constraint, for example if it is in a horizontal scrollable, it will try to shrink-wrap its children along the horizontal axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the horizontal direction.

These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.

image.png

这段代码报的错,Row中嵌套row,嵌套row中有Expand,报错!

解决方法:
在里面的Row外层包上Flexible就OK了。

代码如下:

Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Flexible(
                child: Row(
                  children: [
                    Text(Utils.stringBlank(name), style: AppText.textBold14,),
                    Gaps.hGap12,
                    Expanded(
                        child: Text('23145674895354678579809-0=',
                          style: AppText.textGray3_14,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        )),
                  ],
                ),
              ),
              Visibility(
                  visible: widget.provider.pageType!=2,
                  child:
              )
            ],
          ),

7现在.1 Mac Android Studio 真机运行安卓报错

各种报错,版本不匹配等等,Android 的运行是在是个坑啊,一堆环境配置的,一环套一环。
我下载了好几个版本来回试,始终没有成功,

image.png

后来删了几个就留了上面几个,但是一顿操作猛如虎,结果还是凉凉,对安卓真是一窍都不太通,最后报这个错:

Flutter Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.

前面也报错过,一直没改好,意思是该版本工具已损坏,删除并尝试重新下载,但是删除重新下载也不好使,照样问题还在,确实下载之后文件中存在d8.bat和d8.jar,只需将其改正即可。
进入SDK目录,/Users/xxx/Library/Android/sdk,

/Users/xxx/Library/Android/sdk/build-tools/31.0.0/dx,目录下
将d8.bat——>dx.bat
lib中d8.jar——>dx.jar

并且只有一部分的版本下载会出问题,我下载的版本都有问题,30.0.3以上都会报错,因为30.0.3下的版本SDK有dx的文件。

image.png

rebuild 居然成功了:

image.png

感觉Android的SDK有点太随意了吧,搞笑,下载下来还有问题,还可以这样改来改去的,真的是不可描述,给初学者挖坑呢。

7.2 单独运行安卓项目报错

A problem occurred evaluating project ':app'.> Failed to apply plugin 'com.android.internal.application'.> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.

image.png

解决方法:

image.png

在菜单File->Project Structure->Gradle Settings,选择合适的jdk,
修改适合的版本就好了

7.3 Flutter直接运行还是会报错

A problem occurred evaluating project ':app'.> Failed to apply plugin 'com.android.internal.application'.> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8. Your current JDK is located in /Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre You can try some of the following options: - changing the IDE settings. - changing the JAVA_HOME environment variable. - changingorg.gradle.java.homeingradle.properties.

image.png

Flutter的在File下设置一通,发现还是报错,

image.png

结果百度一通发现,可以改变环境变量,上面其实都提示了,但是IDE(集成开发环境(IDE,Integrated Development Environment ))中找不到设置对的地方,只能修改Flutter SDK 环境变量与 jdk 配置了:
直接用文本编辑打开.bash_profile,不想用vim编辑器,太麻烦了,简单粗暴明了:
/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
这个是系统默认的安装目录,最新的在Android Studio.app的包里contents下:

# Java
# 系统默认安装目录
#export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

注意Android Studio.app名字中间有空格要用反斜杠隔开,修改完了用终端再重新加载一下:

source .bash_profile

就OK了。

你可能感兴趣的:(Flutter 报错问题小结)