手势捕获异常:堆栈溢出
Exception caught by gesture: Stack Overflow
每次我点击 ListView 卡片时,都会出现“手势捕获异常:堆栈溢出”的错误,如屏幕截图所示(https://www.screencast.com/t/udA8NZZ1AqsD)
这里的问题出在 Gesturedetector 的 onTap 代码中
截图列表
默认导航 PageRoute -> https://www.screencast.com/t/qKZQgaraFi
只有一个名为 'test' -> https://www.screencast.com/t/XOxGkANLZEjC 的纯文本
略微更改了导航页面路由 -> https://www.screencast.com/t/1F82pyQtQ2v9
带有新附加代码的手势检测器 -> https://www.screencast.com/t/hRefONV3o
从 firebase 传递数据的代码 -> https://www.screencast.com/t/24gc9zqfxmGX
结果Link:
结果 #1 -> https://www.screencast.com/t/PmiC5U6S2L
结果 #2 -> https://www.screencast.com/t/udA8NZZ1AqsD
我试过下面的方法
试一试
尝试了默认的导航页面路由,并且 Gesturedetector 工作正常。缺点是我只能显示文本(请参阅上面的 link,只有一个名为 'test' 的纯文本)并且无法使用提供程序包 flutter 传递数据,如屏幕截图所示
默认导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
},
)
只有一个名为 'test'
的纯文本
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #1
尝试 2
尝试了稍微改变的导航页面路由(参见上面的 link 'Slightly changed Navigation Page Route')和普通文本(参见上面的 link = With Just a Plain text called 'test')
导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
只有一个名为 'test'
的纯文本
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #1
尝试 3
尝试使用新的附加代码(参见 'Gesturedetector with new additional code' 上面的 link)和普通文本(参见上面的 link,只是一个名为 'test' 的纯文本)
导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
itemNotifier.currentProditem = itemNotifier.itemList[index];
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
),
只有一个名为 'test'
的纯文本
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #2
尝试 4
尝试使用新的附加代码(参见 'Gesturedetector with new additional code' 上面的 link)以及从 firebase 传递数据的代码(参见 'Code that passes data from firebase' 上面的 link)
从 Firebase 传递数据的代码
Text(
(itemNotifier.currentProditem.price),
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #2
我的代码
Widget build(BuildContext context) {
ItemNotifier itemNotifier = Provider.of<ItemNotifier>(context);
return new Scaffold(
backgroundColor: Color(0xffdcdcdc),
appBar: new AppBar(
centerTitle: true,
title: new Text(
'Discover Products',
style: TextStyle(
fontSize: 26.0,
fontWeight: FontWeight.w600,
),
),
backgroundColor: Color(0xFF352d5a)
),
body: Padding(
padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: itemNotifier.itemList.length == null ? 0 : itemNotifier.itemList.length,
itemBuilder: (BuildContext context, index) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
itemNotifier.currentProditem = itemNotifier.itemList[index];
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
// Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
},
child: Stack(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(40.0, 5.0, 20.0, 5.0),
height: 140.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: EdgeInsets.fromLTRB(
100.0, 10.0, 20.0, 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment
.spaceBetween,
crossAxisAlignment: CrossAxisAlignment
.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment
.spaceBetween,
children: <Widget>[
Text(
(itemNotifier.itemList[index].price),
style: TextStyle(
fontSize: 24.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
IconButton(
icon: Icon(Icons.favorite),
color: Colors.grey.shade400,
onPressed: () {
setState(() {
print('Go to Favorites');
});
},
),
],
),
Container(
width: 280.0,
child: Text(
(itemNotifier.itemList[index].producttitle),
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
SizedBox(
height: 5.0,
),
],
),
Container(
width: 280.0,
child: Text(
(itemNotifier.itemList[index].description),
style: TextStyle(
color: Colors.grey
),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
),
],
),
),
),
Positioned(
left: 20.0,
top: 15.0,
bottom: 15.0,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: Colors.black26,
offset: Offset(0.0, 2.0),
blurRadius: 6.0,
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image(
width: 110.0,
image: AssetImage(
'image'
),
fit: BoxFit.cover,
),
),
),
),
],
)
);
}
),
),
);
}
}
问题是您在 set 方法中再次递归调用 set 方法(因此堆栈溢出)
set currentProditem(Proditem proditem){
currentProditem = proditem; //forgot the underscore,
// it should be _currentProditem = proditem;
// To change the variables not the setter again
notifylisteners();
}
每次我点击 ListView 卡片时,都会出现“手势捕获异常:堆栈溢出”的错误,如屏幕截图所示(https://www.screencast.com/t/udA8NZZ1AqsD)
这里的问题出在 Gesturedetector 的 onTap 代码中
截图列表 默认导航 PageRoute -> https://www.screencast.com/t/qKZQgaraFi 只有一个名为 'test' -> https://www.screencast.com/t/XOxGkANLZEjC 的纯文本 略微更改了导航页面路由 -> https://www.screencast.com/t/1F82pyQtQ2v9 带有新附加代码的手势检测器 -> https://www.screencast.com/t/hRefONV3o 从 firebase 传递数据的代码 -> https://www.screencast.com/t/24gc9zqfxmGX
结果Link: 结果 #1 -> https://www.screencast.com/t/PmiC5U6S2L 结果 #2 -> https://www.screencast.com/t/udA8NZZ1AqsD
我试过下面的方法
试一试 尝试了默认的导航页面路由,并且 Gesturedetector 工作正常。缺点是我只能显示文本(请参阅上面的 link,只有一个名为 'test' 的纯文本)并且无法使用提供程序包 flutter 传递数据,如屏幕截图所示
默认导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
},
)
只有一个名为 'test'
的纯文本Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #1
尝试 2 尝试了稍微改变的导航页面路由(参见上面的 link 'Slightly changed Navigation Page Route')和普通文本(参见上面的 link = With Just a Plain text called 'test')
导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
只有一个名为 'test'
的纯文本Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #1
尝试 3 尝试使用新的附加代码(参见 'Gesturedetector with new additional code' 上面的 link)和普通文本(参见上面的 link,只是一个名为 'test' 的纯文本)
导航页面路由
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
itemNotifier.currentProditem = itemNotifier.itemList[index];
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
),
只有一个名为 'test'
的纯文本Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #2
尝试 4 尝试使用新的附加代码(参见 'Gesturedetector with new additional code' 上面的 link)以及从 firebase 传递数据的代码(参见 'Code that passes data from firebase' 上面的 link)
从 Firebase 传递数据的代码
Text(
(itemNotifier.currentProditem.price),
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:参见结果 #2
我的代码
Widget build(BuildContext context) {
ItemNotifier itemNotifier = Provider.of<ItemNotifier>(context);
return new Scaffold(
backgroundColor: Color(0xffdcdcdc),
appBar: new AppBar(
centerTitle: true,
title: new Text(
'Discover Products',
style: TextStyle(
fontSize: 26.0,
fontWeight: FontWeight.w600,
),
),
backgroundColor: Color(0xFF352d5a)
),
body: Padding(
padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: itemNotifier.itemList.length == null ? 0 : itemNotifier.itemList.length,
itemBuilder: (BuildContext context, index) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
itemNotifier.currentProditem = itemNotifier.itemList[index];
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
// Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
},
child: Stack(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(40.0, 5.0, 20.0, 5.0),
height: 140.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: EdgeInsets.fromLTRB(
100.0, 10.0, 20.0, 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment
.spaceBetween,
crossAxisAlignment: CrossAxisAlignment
.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment
.spaceBetween,
children: <Widget>[
Text(
(itemNotifier.itemList[index].price),
style: TextStyle(
fontSize: 24.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
IconButton(
icon: Icon(Icons.favorite),
color: Colors.grey.shade400,
onPressed: () {
setState(() {
print('Go to Favorites');
});
},
),
],
),
Container(
width: 280.0,
child: Text(
(itemNotifier.itemList[index].producttitle),
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold
),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
SizedBox(
height: 5.0,
),
],
),
Container(
width: 280.0,
child: Text(
(itemNotifier.itemList[index].description),
style: TextStyle(
color: Colors.grey
),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
),
],
),
),
),
Positioned(
left: 20.0,
top: 15.0,
bottom: 15.0,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: Colors.black26,
offset: Offset(0.0, 2.0),
blurRadius: 6.0,
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image(
width: 110.0,
image: AssetImage(
'image'
),
fit: BoxFit.cover,
),
),
),
),
],
)
);
}
),
),
);
}
}
问题是您在 set 方法中再次递归调用 set 方法(因此堆栈溢出)
set currentProditem(Proditem proditem){
currentProditem = proditem; //forgot the underscore,
// it should be _currentProditem = proditem;
// To change the variables not the setter again
notifylisteners();
}