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

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

image.png

一堆这个报错:

image.png

<unknown>: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'

后记:
Specs satisfying theJCore (< 3.0.0, = 2.6.2, >= 2.0.0), JCore (= 2.6.2), JCore (= 4.2.1)dependency were found, but they required a higher minimum deployment target.

Specs satisfying theJCore (< 3.0.0, = 2.6.2, >= 2.0.0), JCore (= 4.2.1)dependency were found, but they required a higher minimum deployment target.

image.png

解决这个问题:
修改引用同一个插件的podspec中的版本号,修改为低版本的,重新pod install 就可以了。

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

JDK位置在菜单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了。

8、 Flutter 使用Stack加载图片展示不全问题

问题

使用Stack加载图片,发现一直变形,无论怎么更改fit: BoxFit.fill,还没有效果,加载的时候还跳动。

解决方法

修改Stack的fit属性为StackFit.expand就加载正常了,默认是fit: StackFit.loose

9、 CocoaPods could not find compatible versions for pod "sqflite":

image.png

pod 不支持,删除引用的插件就好了

10、 shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

image.png

目录不存在了,关掉终端,重新进入目录就好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容