Flutter編譯問題
1.Flutter編譯時一直卡在"Resolving dependencies"
原因:國內被牆的原因
解決方案1:
配置系統環境變量:
-
PUB_HOSTED_URL
->https://pub.flutter-io.cn
-
FLUTTER_STORAGE_BASE_URL
->https://storage.flutter-io.cn
最新的變量地址請訪問:https://flutter.io/community/china
解決方案2:
在android
目錄的build.gradle
下添加阿里鏡像代理:
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
google()
jcenter()
}
}
解決方案3:
使用翻牆工具構建
2.運行一些開源的項目編輯報錯提示Finished with error: FormatException: Bad UTF-8 encoding 0xc3 (at offset 169)
原因:app的release版本找不到keystore文件
解決方案:
找到android
->app
目錄下的build.gradle
,找到buildTypes
,修改release
的signingConfigs.release
爲debug
:
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.debug
}
}
或者自己重新配置release
的簽名
Flutter開發問題
1.使用Navigator.push()
跳轉界面報錯:
'Navigator operation requested with a context that does not include a Navigator.\n'
'The context used to push or pop routes from the Navigator must be that of a '
'widget that is a descendant of a Navigator widget.'
原因:傳入的context
不支持堆棧管理,比如一個類直接繼承自StatelessWidget
或StatefulWidget
,而它本身或父類沒有堆棧管理功能。那麼這個類是不支持頁面跳轉的,所以就會報錯。要查看是否支持頁面跳轉,直接進入源碼,比如MaterialApp
類,它是支持的,那麼它的源碼裏會有對應的功能介紹:
/// See also:
///
/// * [Scaffold], which provides standard app elements like an [AppBar] and a [Drawer].
/// * [Navigator], which is used to manage the app's stack of pages. //這裏做了對應的介紹
/// * [MaterialPageRoute], which defines an app page that transitions in a material-specific way.
/// * [WidgetsApp], which defines the basic app elements but does not depend on the material library.
/// * The Flutter Internationalization Tutorial,
/// <https://flutter.io/tutorials/internationalization/>.
class MaterialApp extends StatefulWidget {
.....
}
放一個報錯的代碼:
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Welcome to Flutter',
home: new Scaffold(
body:Container(
child: FlatButton(onPressed:(){
var push = Navigator.push(context,MaterialPageRoute(builder: (BuildContext context) {
return new OtherPager("我是從main傳過去的值");
}));
push.then((value){
print("我是從上個頁面傳遞回來的值$value");
});
}, child:new Icon(Icons.build)),
)
)
);
}
}
這裏的MyApp
是直接繼承自StatelessWidget
的,而在runApp()
中直接使用了它,它沒有父類而它本身也不支持界面的跳轉,所以用它的context
傳入到Navigator
中肯定是會報錯的
解決方案
使用一個有堆棧管理功能的父類將MyApp
類包裹,使其具有堆棧跳轉功能
見如下代碼:
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
body: Container(
child: FlatButton(
onPressed: () {
var push = Navigator.push(context,
MaterialPageRoute(builder: (BuildContext context) {
return new OtherPager("我是從main傳過去的值");
}));
push.then((value) {
print("我是從上個頁面傳遞回來的值$value");
});
},
child: new Icon(Icons.build)),
));
}
}
代碼中將MaterialApp
移出來了,直接將MyApp
包裹了,那麼MyApp
的context
就生效了,代碼也不會報錯啦。