每次我点击ListView卡时,此错误都会显示“此手势捕获了异常:堆栈溢出”(https://www.screencast.com/t/udA8NZZ1AqsD)
问题出在Gesturedetector的onTap代码中
截图列表 默认导航PageRoute-> https://www.screencast.com/t/qKZQgaraFi 仅用一个纯文字称为“测试”-> https://www.screencast.com/t/XOxGkANLZEjC 导航页面路线略有更改-> https://www.screencast.com/t/1F82pyQtQ2v9 具有新的附加代码的Gesturedetector-> https://www.screencast.com/t/hRefONV3o 从Firebase传递数据的代码-> https://www.screencast.com/t/24gc9zqfxmGX
结果链接: 结果#1-> https://www.screencast.com/t/PmiC5U6S2L 结果#2-> https://www.screencast.com/t/udA8NZZ1AqsD
我在下面尝试了以下方法
尝试1 尝试使用默认的“导航页面路线”,并且Gesturedetector运行正常。缺点是我只能显示文本(请参阅上面的链接,只有纯文本称为“ test”),并且不能使用提供程序包flutter传递数据,如此屏幕截图所示
默认导航PageRoute
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetail()));
},
)
只有纯文本称为“测试”
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:请参见结果#1
尝试2 尝试将略有更改的导航页面路线(请参阅上面的“轻微更改导航页面路线”链接)与普通文本一起使用(请参阅上面的链接=仅使用纯文本称为“测试”)
导航页面路线
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
只有纯文本称为“测试”
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:请参见结果#1
尝试3 尝试使用新的附加代码(请参见上面的“带有新附加代码的Gesturedetector”链接)和普通文本(请参见上面的仅用纯文本称为“ test”的链接)
导航页面路线
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
itemNotifier.currentProditem = itemNotifier.itemList[index];
Navigator.of(context).push(
MaterialPageRoute (builder: (BuildContext context) {
return ProductDetail();
})
);
},
),
只有纯文本称为“测试”
Text(
'test',
style: TextStyle(
fontSize: 35.0,
color: Colors.black45,
fontWeight: FontWeight.w700
),
),
结果:请参见结果#2
尝试4 尝试使用新的附加代码(请参阅“带有新附加代码的Gesturedetector上面的链接”)以及传递来自firebase的数据的代码(请参见上方的“传递来自firebase的数据的代码”的链接)
从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,
),
),
),
),
],
)
);
}
),
),
);
}
}
答案 0 :(得分:1)
问题是您要在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();
}