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