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
就生效了,代码也不会报错啦。