文章目录
- 1. 使用路由跳转时报错 Navigator operation requested with a context that does not include a Navigator.
- 2.MediaQuery.of() called with a context that does not contain a MediaQuery
- 3. Exception: ideviceinfo return an error: ERROR: Could not connect to lockdownd, error code ==-18==
- 4./Users/blurfir/flutter/bin/flutter: line 46: /Users/blurfir/flutter/bin/cache/dart-sdk/bin/pub: No such file or directory
- 5. Navigator.pop(context);返回上一级时,页面变为黑色
- 5.type int is not a subtype of type string
- 6.RangeError (index): Invalid value: Valid value range is empty: 0
- 7.弹出软键盘时 报布局溢出错误
- 8. 编译时出现 GC overhead limit exceeded
1. 使用路由跳转时报错 Navigator operation requested with a context that does not include a Navigator.
解决方法:将Scaffold分离出来
原代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "hello",
home: new Scaffold(
appBar: new AppBar(
title: new Text("My App Bar"),
),
body:
//...
children: <Widget>[
RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return DemoApp();
}));
},
),
//...
}
}
修改后代码
lass MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "hello",
home: new HomeWidget(),
);
}}
class HomeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: new Text("My App Bar"),
),
//..
],
),
],
));
}
}
2.MediaQuery.of() called with a context that does not contain a MediaQuery
如果要使用MediaQuery 必须在MaterialApp 中使用
解决方法
Scaffold外层嵌套一个MaterialApp
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
//....
)
)
}
3. Exception: ideviceinfo return an error: ERROR: Could not connect to lockdownd, error code -18
解决方法
运行的时候电脑连有真机 把真机拔下来即可
4./Users/blurfir/flutter/bin/flutter: line 46: /Users/blurfir/flutter/bin/cache/dart-sdk/bin/pub: No such file or directory
Error: Unable to ‘pub upgrade’ flutter tool. Retrying in five seconds… (9 tries left)
解决方法
flutter官网下载Flutter Beta版 ,之前下的Stable版缺文件
5. Navigator.pop(context);返回上一级时,页面变为黑色
原代码
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
return MaterialApp(
home: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
// backgroundColor: Colors.grey,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios,color: Colors.white,), onPressed: (){
Navigator.pop(context);
}),
原因
Navigator中的context对应的是headerSliverBuilder中的context,需要返回上一级页面时,Navigator中的参数应该为build中的context
解决方法
@override
//修改1
Widget build(BuildContext buildcontext) {
final size = MediaQuery.of(context).size;
final width = size.width;
final height = size.height;
return MaterialApp(
home: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
// backgroundColor: Colors.grey,
leading: IconButton(
icon: Icon(Icons.arrow_back_ios,color: Colors.white,), onPressed: (){
//修改2
Navigator.pop(buildcontext);
}),
5.type int is not a subtype of type string
取数据的时候报这个错误
意思是应该填String类型值的地方填了Int
原代码
_GetPlaceAlbum(){
HttpUtil.post(
url: UrlContact.PLACE_ALBUM,
params: { "page_num":'1',
"limit":'20',
//此处的livehouse_id 传入的值应为String 但arguments['id']为int型
"livehouse_id":arguments['id']}).then((r){
String code = r['code'];
if('200'==code){
setState(() {
photos = r['data']['photo'];
print("get first photo"+photos[1]);
});
}
});
}
解决方法
只需要 把该类型转换为int型即可
_GetPlaceAlbum(){
HttpUtil.post(
url: UrlContact.PLACE_ALBUM,
params: { "page_num":'1',
"limit":'20',
"livehouse_id":arguments['id'].toString()}).then((r){
String code = r['code'];
if('200'==code){
setState(() {
photos = r['data']['photo'];
print("get first photo"+photos[1]);
});
}
});
}
6.RangeError (index): Invalid value: Valid value range is empty: 0
现象:页面传完参数后模拟器出现短暂的红色页面,console输出报错RangeError (index): Invalid value: Valid value range is empty: 0
原因
页面接受参数之前 某个组件值为空 所以报错。当页面接收到参数之后,组件成功渲染=>完成显示
解决方法
组件渲染时加一个判断 如果参数为空 输出空Container 或者加一个加载页面 等待参数传递完成
7.弹出软键盘时 报布局溢出错误
原因
直接使用的Scaffold布局
解决方法
在body内的组件外层嵌套一个SingleChildScrollView
@override
Widget build(BuildContext context) {
ScreenUtil.getInstance()..init(context);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('手机号登陆'),
backgroundColor: Colors.white,
actions: <Widget>[
Container(
margin: EdgeInsets.only(right: ScreenUtil().setWidth(15)),
child: Center(
child: GestureDetector(
onTap: (){
Navigator.pushReplacementNamed(context, '/login/password');
},
child: Text('密码登录'),
),
),
)
],
),
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: _buildLoginBody(),
),
);
}
8. 编译时出现 GC overhead limit exceeded
原因
编译内存满载
解决方法
方法一: 修改项目目录下的gradle.properties,增加如下配置信息(红色文字中需要根据自己电脑的配置修改内存大小,其余的配置用于加快gradle的编译速度)
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
方法二:修改应用目录下的build.gradle配置,在android{}中增加如下配置
dexOptions {
javaMaxHeapSize “4096M”
}
或
dexOptions {
javaMaxHeapSize “4g”
}