Flutter 報(bào)錯(cuò)問(wèn)題小結(jié)

前言

本篇主要記錄一下在學(xué)習(xí)Flutter的過(guò)程中遇到的一些問(wèn)題,及解決方法,菜鳥(niǎo)學(xué)習(xí),持續(xù)更新中...

1. Flutter添加圖片的踩坑過(guò)程

今天添加圖片一直報(bào)錯(cuò):

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

這是因?yàn)閳D片文件夾目錄路徑不對(duì),要放在項(xiàng)目目錄也就是Lib文件夾下:

image.png

在Flutter中,所有的圖片資源與自定義字體資源,默認(rèn)情況下都需要添加在Flutter應(yīng)用根目錄下的assets目錄下(assets文件目錄需要用戶(hù)自己新建,名稱(chēng)必須是“assets”,否則會(huì)報(bào)找不到圖片的錯(cuò)誤),并且需要在項(xiàng)目的pubspec.yaml文件下對(duì)應(yīng)的位置的添加圖片名稱(chēng)。

然后放開(kāi) pubspec.yaml 中 assets 里的注釋?zhuān)瑑?nèi)容改成如圖所示的,但是依然報(bào)錯(cuò),這是因?yàn)榕渲梦募?,pubspec.yaml 中未對(duì)齊,層次不對(duì),注意“-”前后的空格,F(xiàn)lutter對(duì)空格有很?chē)?yán)格的要求,

image.png

對(duì)齊以后就好了:

image.png

碼代碼,使用 Card 組件添加一個(gè)圖片

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(
        // 應(yīng)用頂欄
        appBar: AppBar(
          title: Text("Hello World"),
        ),
        body: Card(
          child: Column(
            children: [Image.asset('asset/images/lake.png'), Text('添加圖片')],
          ),
        ),
      ),
    );
  }
}

但是依舊報(bào)錯(cuò),讀不到這個(gè)圖片,

image.png

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

assets:
    - assets/images/lake.png

把Assets 文件夾移出來(lái)和lib平級(jí),pubspec.yaml 中寫(xiě)不寫(xiě)圖片名字都可以。

image.png

2. 運(yùn)行報(bào)錯(cuò)問(wèn)題如下:

終端中的報(bào)錯(cuò)信息:

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 中的報(bào)錯(cuò)信息:
Command CompileSwift failed with a nonzero exit code

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

image.png

一堆這個(gè)報(bào)錯(cuò):

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項(xiàng)目中導(dǎo)入了二維碼掃描包 和 amap_special 地圖

  # 掃碼
  barcode_scan: ^3.0.1

然后在Android端和web是沒(méi)有問(wèn)題的,但是在iOS運(yùn)行就會(huì)報(bào)上面的錯(cuò)誤,既然是外接包方面的那就是pods上面的問(wèn)題了,網(wǎng)上百度了很多,但是沒(méi)能解決問(wèn)題,也有講OC項(xiàng)目和Swift語(yǔ)言版本的問(wèn)題,后來(lái)也一直以為是語(yǔ)言的問(wèn)題呢,還有pod clean 再install的,更改Build Setting設(shè)置的,各種方式,嘗試了一遍也沒(méi)解決,

我看網(wǎng)上還有說(shuō)下面barcode_scan這個(gè)報(bào)錯(cuò)的,在profile 文件中添加 use_frameworks!然后pod install,是的,這樣可以解決這個(gè)問(wèn)題,但是地圖的依然報(bào)錯(cuò):

image.png

直到今天下午再一位熱心同事的幫助下才解決了問(wèn)題,說(shuō)到底其實(shí)很簡(jiǎn)單,下面的報(bào)錯(cuò)信息已經(jīng)提示的很明白了,奈何不好好的仔細(xì)研究,混亂一通的百度,事倍功無(wú)。

不想看上面踩坑歷程的,直接看這里:

解決方法:

只需要這樣:

#use_frameworks!
use_modular_headers!

明白了吧?So easy!運(yùn)行OK,折磨半天的問(wèn)題終于解決了,特此記錄一下,給踩坑的小伙伴做個(gè)參考。

3. 引用庫(kù)工程,庫(kù)工程引入第三方插件,插件中引入靜態(tài)庫(kù)

use_frameworks!報(bào)錯(cuò)找不到文件,改用use_modular_headers!,直接崩潰,控制臺(tái)報(bào)錯(cuò)如下:

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

運(yùn)行OK,解決了

4. Flutter引用jpush_flutter: 2.4.0第三方插件,iOS運(yùn)行pod install 報(bào)錯(cuò),如下:

[!] 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

這是因?yàn)镕lutter引用了極光的兩個(gè)插件 janalytics: 2.1.5和jpush_flutter: 2.4.0,引用的JCore版本不一致導(dǎo)致的。

解決:

將jpush_flutter的JCore模塊降級(jí)版本:

點(diǎn)開(kāi)ios目錄下.symlinks/plugins,打開(kāi)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

解決這個(gè)問(wèn)題:
修改引用同一個(gè)插件的podspec中的版本號(hào),修改為低版本的,重新pod install 就可以了。

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

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

解決方法

今天一直報(bào)各種錯(cuò)誤,這個(gè)錯(cuò)誤發(fā)現(xiàn)每次運(yùn)行Xcode都需要clean才行,不徹底,清除緩存,重啟,然后在flutter項(xiàng)目目錄下執(zhí)行 flutter clean 命令,重啟,重新運(yùn)行。

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

這段代碼報(bào)的錯(cuò),Row中嵌套row,嵌套row中有Expand,報(bào)錯(cuò)!

解決方法:
在里面的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現(xiàn)在.1 Mac Android Studio 真機(jī)運(yùn)行安卓報(bào)錯(cuò)

各種報(bào)錯(cuò),版本不匹配等等,Android 的運(yùn)行是在是個(gè)坑啊,一堆環(huán)境配置的,一環(huán)套一環(huán)。
我下載了好幾個(gè)版本來(lái)回試,始終沒(méi)有成功,

image.png

后來(lái)刪了幾個(gè)就留了上面幾個(gè),但是一頓操作猛如虎,結(jié)果還是涼涼,對(duì)安卓真是一竅都不太通,最后報(bào)這個(gè)錯(cuò):

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

前面也報(bào)錯(cuò)過(guò),一直沒(méi)改好,意思是該版本工具已損壞,刪除并嘗試重新下載,但是刪除重新下載也不好使,照樣問(wèn)題還在,確實(shí)下載之后文件中存在d8.bat和d8.jar,只需將其改正即可。
進(jìn)入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

并且只有一部分的版本下載會(huì)出問(wèn)題,我下載的版本都有問(wèn)題,30.0.3以上都會(huì)報(bào)錯(cuò),因?yàn)?0.0.3下的版本SDK有dx的文件。

image.png

rebuild 居然成功了:

image.png

感覺(jué)Android的SDK有點(diǎn)太隨意了吧,搞笑,下載下來(lái)還有問(wèn)題,還可以這樣改來(lái)改去的,真的是不可描述,給初學(xué)者挖坑呢。

7.2 單獨(dú)運(yùn)行安卓項(xiàng)目報(bào)錯(cuò)

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直接運(yùn)行還是會(huì)報(bào)錯(cuò)

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下設(shè)置一通,發(fā)現(xiàn)還是報(bào)錯(cuò),

image.png

結(jié)果百度一通發(fā)現(xiàn),可以改變環(huán)境變量,上面其實(shí)都提示了,但是IDE(集成開(kāi)發(fā)環(huán)境(IDE,Integrated Development Environment ))中找不到設(shè)置對(duì)的地方,只能修改Flutter SDK 環(huán)境變量與 jdk 配置了:
直接用文本編輯打開(kāi).bash_profile,不想用vim編輯器,太麻煩了,簡(jiǎn)單粗暴明了:
/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
這個(gè)是系統(tǒng)默認(rèn)的安裝目錄,最新的在Android Studio.app的包里contents下:

# Java
# 系統(tǒng)默認(rèn)安裝目錄
#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名字中間有空格要用反斜杠隔開(kāi),修改完了用終端再重新加載一下:

source .bash_profile

就OK了。

8、 Flutter 使用Stack加載圖片展示不全問(wèn)題

問(wèn)題

使用Stack加載圖片,發(fā)現(xiàn)一直變形,無(wú)論怎么更改fit: BoxFit.fill,還沒(méi)有效果,加載的時(shí)候還跳動(dòng)。

解決方法

修改Stack的fit屬性為StackFit.expand就加載正常了,默認(rèn)是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

目錄不存在了,關(guān)掉終端,重新進(jìn)入目錄就好了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容