Flutter 從入門(mén)到放棄(Mac)

Read Me

  1. Mac環(huán)境安裝很方便较雕,跟著flutter網(wǎng)站走一般不會(huì)迷路贮尖,這里就不贅絮了。主要記錄一些問(wèn)題和開(kāi)發(fā)中需要注意的地方
  2. 之前是iOS開(kāi)發(fā)丁稀,有些基礎(chǔ),所以Android的問(wèn)題會(huì)相對(duì)多一些棘捣,也容易犯一些簡(jiǎn)單的錯(cuò)誤砌些,主要還是記錄給自己看啥寇,能幫到一些跟我一樣沒(méi)基礎(chǔ)的人最好了
  3. 文章暫定分為五個(gè)部分:Read Me胡诗,常用指令,Android問(wèn)題逛球,iOS問(wèn)題氯葬,F(xiàn)lutter問(wèn)題。每個(gè)問(wèn)題主要分三部分:?jiǎn)栴}描述衙荐,問(wèn)題原因煌抒,解決辦法
  4. 持續(xù)更新棒仍,發(fā)現(xiàn)長(zhǎng)時(shí)間沒(méi)有更新大概就是放棄的時(shí)候了吧

這里記錄一些常用的指令

//診斷flutter運(yùn)行需要環(huán)境
flutter docter
//清除緩存
flutter clean
//打包Android APK,這里加上--no-tree-shake-icons是加載資源時(shí)報(bào)錯(cuò),下面有提到
flutter build apk --no-tree-shake-icons

Android

1.第一次失敗的運(yùn)行

問(wèn)題描述:
?  happy git:(dev) ? flutter run                              
Using hardware rendering with device sdk gphone x86 arm. If you notice graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
Launching lib/main.dart on sdk gphone x86 arm in debug mode...
/happy/android/app/src/debug/AndroidManifest.xml:4:9-43 Error:
        Attribute application@label value=(medical_management) from AndroidManifest.xml:4:9-43
        is also present at [com.tencent.imsdk:imsdk-smart:5.2.210] AndroidManifest.xml:18:9-41 value=(@string/app_name).
        Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:3:4-40:19 to override.
                                                                        
FAILURE: Build failed with an exception.                                
                                                                        
* What went wrong:                                                      
Execution failed for task ':app:processDebugMainManifest'.              
> Manifest merger failed : Attribute application@label value=(medical_management) from AndroidManifest.xml:4:9-43
        is also present at [com.tencent.imsdk:imsdk-smart:5.2.210] AndroidManifest.xml:18:9-41 value=(@string/app_name).
        Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:3:4-40:19 to override.
                                                                        
* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
                                                                        
* Get more help at https://help.gradle.org                              
                                                                        
BUILD FAILED in 22s   
問(wèn)題原因:

據(jù)百度描述這個(gè)問(wèn)題是項(xiàng)目application的label屬性沖突導(dǎo)致,這是一個(gè)編譯的常見(jiàn)錯(cuò)誤鳍悠,主要是AS的Gradle插件默認(rèn)會(huì)啟用Manifest Merger Tool,若Library項(xiàng)目中也定義了與主項(xiàng)目相同的屬性(例如默認(rèn)生成的android:icon和android:theme)却紧,則此時(shí)會(huì)合并失敗介汹,并報(bào)上面的錯(cuò)誤。至于這是個(gè)什么屬性,我也不是太了解否灾,以后需要加強(qiáng)Android知識(shí)的學(xué)習(xí)墨技、

解決辦法:

在項(xiàng)目的Manifest.xml文件里的application中加入

xmlns:tools="http://schemas.android.com/tools"
tools:replace="label"

親測(cè)有效~

2. 第一次打包報(bào)錯(cuò)
①問(wèn)題描述:
?  VIP-DOCTOR-NURSE-APP git:(dev) ? flutter build apk

Building without sound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety

注: 某些輸入文件使用或覆蓋了已過(guò)時(shí)的 API恐锣。                              
注: 有關(guān)詳細(xì)信息, 請(qǐng)使用 -Xlint:deprecation 重新編譯。                  
注: 某些輸入文件使用了未經(jīng)檢查或不安全的操作给涕。                          
注: 有關(guān)詳細(xì)信息, 請(qǐng)使用 -Xlint:unchecked 重新編譯。                    
This application cannot tree shake icons fonts. It has non-constant instances of IconData at the following locations:
問(wèn)題原因:

百度發(fā)現(xiàn)是版本問(wèn)題 flutter 升級(jí)最新版本 使用 treeview 打包報(bào)錯(cuò)

1.打包時(shí)加上 --no-tree-shake-icons
2.將flutter sdk 版本退回到穩(wěn)定版本 (生產(chǎn)建議用 穩(wěn)定版 准夷,钥飞。。衫嵌。读宙。。楔绞。)

②Keystore was tampered with, or password was incorrect
FAILURE: Build failed with an exception.                                
                                                                      
* What went wrong:                                                      
Execution failed for task ':app:packageRelease'.                        
> A failure occurred while executing com.android.build.gradle.tasks.PackageAndroidArtifact$IncrementalSplitterRunnable
 > com.android.ide.common.signing.KeytoolException: Failed to read key sign from store "/Users/ytwd/項(xiàng)目/VIP-DOCTOR-NURSE-APP/android/app/sign.jks": Keystore was tampered with, or password was incorrect
                                                                      
* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

問(wèn)題原因:

生成sign.jks的時(shí)候秘鑰混淆了结闸,注意這里有兩個(gè)pass


20200327173214447.png
解決辦法:

key.properties 這個(gè)文件填寫(xiě)正確的信息即可

storePassword=123456
keyPassword=123456
keyAlias=sign
storeFile=sign.jks

# storeFile 簽名文字
# keyAlias  文件的別名
# storepass 123456   這個(gè)是在創(chuàng)建簽名的時(shí)候輸入的  秘鑰庫(kù)秘鑰
# keyPassword 123456    為設(shè)置的秘鑰串
暫時(shí)出現(xiàn)上面兩個(gè)問(wèn)題,有問(wèn)題繼續(xù)更新酒朵,到這里就打包成功了
?  VIP-DOCTOR-NURSE-APP git:(dev) ? flutter build apk --no-tree-shake-icons

Building without sound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety

Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                       9.8s
? Built build/app/outputs/flutter-apk/app-release.apk (30.4MB).

Flutter

1. Expanded 布局最常見(jiàn)問(wèn)題
問(wèn)題描述:
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type StackParentData.

Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a Stack widget.

The ownership chain for the RenderObject that received the incompatible parent data was:
  Padding ← Container ← Expanded ← Stack ← Column ← Padding ← Container ← Expanded ← Row ← Padding ← ?
When the exception was thrown, this was the stack
#0      RenderObjectElement._updateParentData.<anonymous closure>
package:flutter/…/widgets/framework.dart:5626
#1      RenderObjectElement._updateParentData
package:flutter/…/widgets/framework.dart:5642
#2      RenderObjectElement.attachRenderObject
package:flutter/…/widgets/framework.dart:5664
#3      RenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5357
#4      SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5973
問(wèn)題原因:

錯(cuò)誤信息已經(jīng)給的比較清楚的桦锄,就是Expanded組件父試圖錯(cuò)誤
看錯(cuò)誤代碼

new Expanded(
                          child: new Container(
                              padding: EdgeInsets.only(right: 38.0),
                              child: new Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  new Stack(
                                    children: <Widget>[
                                      new Expanded(
                                          child: new Container(
                                              padding:
                                                  EdgeInsets.only(left: 15.0),
                                              child: new Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: <Widget>[
                                                  new Stack(
                                                    children: <Widget>[
                                                      _rowMessgeView(message),
                                                    ],
                                                  )
                                                ],
                                              ))),
                                    ],
                                  )
                                ],
                              ))),
解決辦法:

只要記住一點(diǎn)Expanded的上級(jí)控件一定是Column或者Row就好了

2. Flutter更新showDialog中的內(nèi)容
問(wèn)題描述:

在用showDialog的時(shí)候應(yīng)該都遇到過(guò)這個(gè)問(wèn)題,使用showDialog后蔫耽,通過(guò)setState()無(wú)法更新當(dāng)前dialog

問(wèn)題原因:

因?yàn)閐ialog其實(shí)是另一個(gè)頁(yè)面结耀,準(zhǔn)確地來(lái)說(shuō)是另一個(gè)路由,因?yàn)閐ialog的關(guān)閉也是通過(guò)navigator來(lái)pop的匙铡,所以它的地位跟你當(dāng)前主頁(yè)面一樣图甜。這個(gè)概念一定要明確,因?yàn)闊o(wú)論在Android或iOS中鳖眼,daliog都是依附于當(dāng)前主頁(yè)面的一個(gè)控件具则,但是在Flutter中不同,它是一個(gè)新的路由具帮。所以使用當(dāng)前主頁(yè)面的setState()來(lái)更新博肋,當(dāng)然沒(méi)法達(dá)到你要的效果。

解決辦法:

所以我們有兩種方法來(lái)解決這個(gè)問(wèn)題蜂厅,一種是使用StatefulBuilder匪凡,另一種是使用自定義的StatefulWidget。
這里只用StatefulBuilder掘猿,代碼如下:


showDialog(
    context: context,
    builder: (context) {
        String label = 'test';
        return StatefulBuilder(
            builder: (context, state) {
                print('label = $label');
                return GestureDetector(
                    child: Text(label),
                    onTap: () {
                        label = 'test8';
                        print('onTap:label = $label');
                        // 注意不是調(diào)用老頁(yè)面的setState病游,而是要調(diào)用builder中的setState。
                        //在這里為了區(qū)分,在構(gòu)建builder的時(shí)候?qū)etState方法命名為了state衬衬。
                        state(() {});  
                    },
                );
            },
         );
    });

//這里直接調(diào)用state(() {}) 代替setState(() {})
3. Flutter動(dòng)畫(huà)生命周期問(wèn)題
問(wèn)題描述:
Unhandled Exception: _ChatViewPageState#1e2cd(tickers: tracking 1 ticker) was disposed with an active Ticker.

_ChatViewPageState created a Ticker via its TickerProviderStateMixin, but at the time dispose() was called on the mixin, that Ticker was still active. All Tickers must be disposed before calling super.dispose().

Tickers used by AnimationControllers should be disposed by calling dispose() on the AnimationController itself. Otherwise, the ticker will leak.

The offending ticker was:

_WidgetTicker(created by _ChatViewPageState#1e2cd(lifecycle state: created, tickers: tracking 0 tickers))

The stack trace when the _WidgetTicker was actually created was:

#0 new Ticker.<anonymous closure>

package:flutter/…/scheduler/ticker.dart:67

#1 new Ticker

package:flutter/…/scheduler/ticker.dart:69

#2 new _WidgetTicker

package:flutter/…/widgets/ticker_provider.dart:271

#3 TickerProviderStateMixin.createTicker (package:flutter/s<…>
4. Flutter組件Image

問(wèn)題描述:

  1. 不能識(shí)別iOS多倍圖买猖。
Image.asset(
            'assets/images/fallback@3x.png',
            scale: 3,
            width: 24,
            height: 24,
          )
5. Flutter切換tabbar時(shí),首次切換圖片閃爍

解決方式:gaplessPlayback參數(shù)設(shè)置true

Image.asset(
                    e.selectImage,
                    scale: 3,
                    color: e.selectColor,
                    excludeFromSemantics: true,
                    gaplessPlayback: true,
                  )
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末滋尉,一起剝皮案震驚了整個(gè)濱河市玉控,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌狮惜,老刑警劉巖高诺,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異碾篡,居然都是意外死亡虱而,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)开泽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)牡拇,“玉大人,你說(shuō)我怎么就攤上這事穆律』莺簦” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵众旗,是天一觀的道長(zhǎng)罢杉。 經(jīng)常有香客問(wèn)我,道長(zhǎng)贡歧,這世上最難降的妖魔是什么滩租? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮利朵,結(jié)果婚禮上律想,老公的妹妹穿的比我還像新娘。我一直安慰自己绍弟,他們只是感情好技即,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著樟遣,像睡著了一般而叼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上豹悬,一...
    開(kāi)封第一講書(shū)人閱讀 49,166評(píng)論 1 284
  • 那天葵陵,我揣著相機(jī)與錄音,去河邊找鬼瞻佛。 笑死脱篙,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绊困,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼文搂,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了秤朗?” 一聲冷哼從身側(cè)響起煤蹭,我...
    開(kāi)封第一講書(shū)人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎川梅,沒(méi)想到半個(gè)月后疯兼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體然遏,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贫途,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了待侵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丢早。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖秧倾,靈堂內(nèi)的尸體忽然破棺而出怨酝,到底是詐尸還是另有隱情,我是刑警寧澤那先,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布农猬,位于F島的核電站,受9級(jí)特大地震影響售淡,放射性物質(zhì)發(fā)生泄漏斤葱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一揖闸、第九天 我趴在偏房一處隱蔽的房頂上張望揍堕。 院中可真熱鬧,春花似錦汤纸、人聲如沸衩茸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)楞慈。三九已至,卻和暖如春啃擦,著一層夾襖步出監(jiān)牢的瞬間囊蓝,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工议惰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慎颗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像俯萎,于是被迫代替她去往敵國(guó)和親傲宜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344