github 上 clone 下來的項(xiàng)目報(bào)錯(cuò)/找不到文件?
1、倉庫 clone 下來后我們執(zhí)行flutter run
Terminal 中提示我們Target file "lib/main.dart" not found.
這時(shí)我們要判斷打開示例項(xiàng)目的路徑對不對驹止,對帶有示例的倉庫钞诡,我們?nèi)绻蜷_演示示例兼贡,需要用 vscode打開示例所在的文件夾,一般是 example 文件夾
2丈攒、在我們使用正確的路徑打開項(xiàng)目后,有很多報(bào)錯(cuò)導(dǎo)入的文件找不到授霸,這個(gè)時(shí)候我們需要在 Terminal 中 執(zhí)行以下flutter pub get
命令巡验,執(zhí)行完導(dǎo)入的一些庫就能夠找到了Terminal 中提示
[!] Your app isn't using AndroidX.
這種情況需要我們在android/gradle.properties
文件中設(shè)置
android.enableJetifier=true
android.useAndroidX=true
gradle 版本不對
- Terminal 中報(bào)錯(cuò)
Minimum supported Gradle version is 5.4.1. Current version is 4.10.2. If using the gradle wrapper, try editing the distributionUrl in /Users/zhangjiwen/Desktop/flutter_k_chart/example/android/gradle/wrapper/gradle-wrapper.properties to gradle-5.4.1-all.zip
解決辦法是:我們在android/gradle/wrapper/gradle-wrapper.properties
中將distributionUrl
中的 gradle zip 包改成我們需要的版本。
例如:沒改之前是distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
修改后:distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
- Terminal 中報(bào)
Exception in thread "main" java.util.zip.ZipException: zip END header not found
這個(gè)錯(cuò)也是 gradle版本不對造成的,將 android -> gradle -> wrapper -> gradlewrapper.properties文件中的 distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip中的 gradle-4.10.2-all.zip 改為與 Flutter 對應(yīng)的版本既可
NoSuchMethodError 總結(jié)
- NoSuchMethodError: The method '***' was called on null
在新增 Provider 狀態(tài)管理之后,我們需要執(zhí)行flutter run
碘耳,如果使用熱重載的話Termianl
中就會報(bào)上面的錯(cuò)誤 - NoSuchMethodError: The method '/' was called on null
盡管在某些頁面設(shè)置過了ScreenUtil.instance
但是切換都某些頁面的時(shí)候還是報(bào)了這個(gè)錯(cuò)显设,這就需要我們在報(bào)錯(cuò)的頁面也設(shè)置一下ScreenUtil.instance
Flutter 模擬器 加載網(wǎng)絡(luò)資源加載不出來的問題
- 需要我們在 DNS 上新加一個(gè) 8.8.8.8
Container 不能同時(shí)設(shè)置color和 decoration,同時(shí)設(shè)置會報(bào)錯(cuò)Cannot provide both a color and a decoration ,the color argument is just a shorthand for 'decoration: new BoxDecotation(color: color)'
模擬器加載不出來網(wǎng)絡(luò)圖片
- 模擬器加載不出來網(wǎng)絡(luò)圖片的表現(xiàn)就是網(wǎng)絡(luò)圖片不顯示辛辨,頁面點(diǎn)擊沒反應(yīng)捕捂,這個(gè)時(shí)候我們需要新增一個(gè)DNS: 8.8.8.8 就好了
網(wǎng)絡(luò)請求的錯(cuò)誤
- 在使用dio進(jìn)行網(wǎng)絡(luò)請求的時(shí)候出現(xiàn)
FormatException: Unexpected character (at character 1)
報(bào)錯(cuò),因?yàn)閐io請求返回的數(shù)據(jù)默認(rèn)是以json的格式讀取的,而返回的數(shù)據(jù)是密文形式斗搞,需要根據(jù)接口設(shè)置設(shè)置dio.options.responseType
绞蹦,這里設(shè)置為dio.options.responseType = ResponseType.plain;
就好了
數(shù)據(jù)解析錯(cuò)誤
-
Exception has occurred. _TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')
這里提示我們接口返回給我們一個(gè)list,但我們以Map去處理了
使用 Provider 報(bào) Tried to listen to a value exposed with provider, from outside of the widget tree
錯(cuò)
在使用Provider提供的 Provider.of<*****>(context,listen: false).xxxxxxx()
; 獲取共享對象時(shí),listen
參數(shù)一定要使用false榜旦,否則就會報(bào)上面的錯(cuò)誤幽七,如果這里不適用。該方法的listen
參數(shù)默認(rèn)是true
; static T of<T>(BuildContext context, {bool listen = true})
這里需要注意的是
:在我們使用Provider.of
修改值的時(shí)候listen
參數(shù)需要設(shè)置為false
溅呢;當(dāng)我們使用Provider.of
獲取值的時(shí)候listen
參數(shù)需要設(shè)置為true
澡屡;listen
參數(shù)到底是使用true
還是使用false
取決與猿挚,當(dāng)取的值變化的時(shí)候控件要不要更新。
VSCode 運(yùn)行flutter項(xiàng)目報(bào)Building for iOS Simulator, but the linked and embedded framework 'App.framework'
的錯(cuò)
- 使用xcode 打開flutter 下的iOS項(xiàng)目找到App.framework 刪除驶鹉,清理緩存绩蜻,重新用xcode編譯就行了
調(diào)用Cupertino樣式警告框報(bào)錯(cuò)
- 使用CupertinoAlertDialog在安卓上報(bào)
The following NoSuchMethodError was thrown building LayoutBuilder: The getter alterDialogLabel was called on null
的錯(cuò) - 解決: 在MaterialApp 的 localizationsDelegates 屬性中添加 FallbackCupertinoLocalisationsDelegate() 項(xiàng),F(xiàn)allbackCupertinoLocalisationsDelegate代碼如下:
MaterialApp(
......
localizationsDelegates: [
FallbackCupertinoLocalisationsDelegate(),
],
......
)
import 'package:flutter/cupertino.dart';
class FallbackCupertinoLocalisationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
const FallbackCupertinoLocalisationsDelegate();
@override
bool isSupported(Locale locale) => true;
@override
Future<CupertinoLocalizations> load(Locale locale) =>
DefaultCupertinoLocalizations.load(locale);
@override
bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}
運(yùn)行安卓模擬器報(bào)A problem occurred configuring project ':path_provider'. > Could not resolve all artifacts for configuration ':path_provider:classpath'.
- 完整報(bào)錯(cuò)信息如下:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':path_provider'.
> Could not resolve all artifacts for configuration ':path_provider:classpath'.
> Could not resolve com.android.tools.build:gradle:3.3.0\.
Required by:
project :path_provider
> Could not resolve com.android.tools.build:gradle:3.3.0\.
> Could not get resource '[https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom](https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom)'.
> Could not GET '[https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom](https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom)'.
> Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Connection refused (Connection refused)
> Could not resolve com.android.tools.build:gradle:3.3.0\.
> Could not get resource '[https://jcenter.bintray.com/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom](https://jcenter.bintray.com/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom)'.
> Could not GET '[https://jcenter.bintray.com/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom](https://jcenter.bintray.com/com/android/tools/build/gradle/3.3.0/gradle-3.3.0.pom)'.
> Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Connection refused (Connection refused)
> Could not get unknown property 'android' for project ':path_provider' of type org.gradle.api.Project.
* 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](https://help.gradle.org)
- 解決過程:
1室埋、首先办绝,我以為是安卓模擬器的問題,我就用安卓模擬器運(yùn)行之前其他的項(xiàng)目沒結(jié)果完美運(yùn)行姚淆,沒有任何問題孕蝉。
2、既然不是安卓模擬器的問題腌逢,那么應(yīng)該就是我們代碼里配置的原因降淮,然后我就分析報(bào)錯(cuò)原因,既然報(bào)錯(cuò)是跟path_provider相關(guān)搏讶,那么我就去搜path_provider相關(guān)的東西佳鳖,發(fā)現(xiàn)我們的pubspec.ymal文件中并沒有導(dǎo)入path_provider
的庫,但是pubspec.lock文件中確有這個(gè)庫媒惕。然后我在網(wǎng)上搜跟path_provider相關(guān)的報(bào)錯(cuò)原因系吩,千奇百怪,各種答案嘗試后還是解決不了問題妒蔚。然后報(bào)錯(cuò)信息還有跟gradle相關(guān)的淑玫,然后我又搜報(bào)錯(cuò)的gradle的信息,各種答案也是解決不了面睛,然后我在想是不是墻的原因絮蒿,然后我代理打開,依舊不行叁鉴,然后我又想到去用阿里云的鏡像土涝,然后還是不行,我有點(diǎn)泄氣了幌墓。目光重新回到path_provider
3但壮、目光重新回到path_provider上,這時(shí)我想到是不是pub沒有找到合適的版本常侣,因?yàn)槲覀円氲娜綆斓陌姹咎柖际菍懰赖睦敲次揖桶阉幸氲膸旄某砂姹究勺兊模缓笾匦逻\(yùn)行還是報(bào)錯(cuò)胳施。
5溯祸、然后我查了一下這個(gè)庫是干什么用的,大概是用來做本地存儲的,我就分析了我們引入的庫里面有沒有需要使用到本地存儲的焦辅。然后發(fā)現(xiàn)我們引入了一個(gè)叫cached_network_image的庫用來做圖片緩存博杖,既然緩存肯定涉及到本地?cái)?shù)據(jù)的操作。
6筷登、我將pubspec.ymal文件中導(dǎo)入cached_network_image的代碼給注釋掉剃根,代碼中用到cached_network_image的代碼都注釋,然后刪除app前方,重新運(yùn)行狈醉,結(jié)果沒錯(cuò)了,安卓模擬器運(yùn)行起來了惠险。這時(shí)我確定應(yīng)該是cached_network_image中引用了path_provider庫導(dǎo)致的苗傅。
7、刪除cached_network_image庫后莺匠,我發(fā)現(xiàn)之前編譯產(chǎn)生的.flutter-plugins 和 .flutter-plugins-dependencies文件也沒有了金吗。
8十兢、這個(gè)時(shí)候我又想趣竣,為什么安卓同事的為什么可以運(yùn)行呢?我猜是他們使用的flutter 版本和dart版本有可能跟我的不一樣導(dǎo)致的旱物,然后我去查看了他們的flutter 版本和dart sdk版本遥缕,結(jié)果我們使用的版本是一樣的。這就很好奇了宵呛,同樣的代碼他們沒問題单匣,我這邊確有問題。那到底問題出在哪里宝穗?
9户秤、第二天我想總結(jié)一下昨天遇到的問題,然后就把昨天注釋掉的cached_network_image庫重新導(dǎo)入逮矛,然后刪除app重新運(yùn)行鸡号,我滿心期待報(bào)錯(cuò)信息的出現(xiàn),結(jié)果须鼎,我靠安卓模擬器啟動了鲸伴,我靠,又正常了晋控,真TM太神奇了汞窗,我也懵了,不知道問題到底出在哪里了赡译。
控制臺輸出setState() or markNeedsBuild() called during build.提示
The following assertion was thrown while dispatching notifications for TradeProvider:
flutter: setState() or markNeedsBuild() called during build.
flutter: This _DefaultInheritedProviderScope<TradeProvider> widget cannot be marked as needing to build
flutter: because the framework is already in the process of building widgets. A widget can be marked as
flutter: needing to be built during the build phase only if one of its ancestors is currently building. This
flutter: exception is allowed because the framework builds parent widgets before children, which means a
flutter: dirty descendant will always be built. Otherwise, the framework might not visit this widget during
flutter: this build phase.
flutter: The widget on which setState() or markNeedsBuild() was called was:
flutter: _DefaultInheritedProviderScope<TradeProvider>
flutter: The widget which was currently being built when the offending call was made was:
flutter: Builder
- 這里是我們在initstatus方法中直接或間接的調(diào)用了setStatus導(dǎo)致的仲吏,出現(xiàn)這種問題是要修改掉了,因?yàn)閕nit的時(shí)候還沒執(zhí)行build方法,第一次執(zhí)行build應(yīng)有系統(tǒng)去調(diào)用的蜘矢。
- 安卓模擬器打不開圖片請使用:8.8.8.8