Flutter 无法在 listtile 内递增,总是返回 0
Flutter cannot increment inside listtile, always returning 0
我想在尾部增加数字 ListTile
Flutter when ontap ListTile,
但总是 return 到 0?
我正在使用 future builder fyi,
提前致谢
this my model
class ItemModel {
String name;
String price;
String image;
bool isSelected = false;
int countSelected = 0;
ItemModel(
this.name,
this.price,
this.image,
);
}
此方法从本地服务器apijson获取数据
Future<List<ItemModel>> _getItems() async {
List<ItemModel> listItems = [];
var url = Uri.parse(BASEURLLOCAL.apiGetItems);
var data = await http.get(url);
var jsonData = jsonDecode(data.body);
for (var p in jsonData) {
ItemModel item = ItemModel(
p["name"],
p["price"],
p["image"],
// p["PRODUCT_NAME"],
// p["PRICE_SELL"],
// p["FILENAME"],
);
listItems.add(item);
}
return listItems;
}
这是列表视图生成器
@override
Widget build(BuildContext context) {
return FutureBuilder<List<ItemModel>>(
future: _getItems(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext contex, int idx) {
if (_itemCount.length < snapshot.data.length) {
_itemCount.add(0);
}
return Card(
child: ListTile(
leading: Image.network(
BASEURLLOCAL.imgItem + snapshot.data[idx].image),
title: Text(snapshot.data[idx].name),
subtitle: Text(snapshot.data[idx].price),
// trailing: _buildTrailing(),
trailing: Text(snapshot.data[idx].countSelected.toString()),
onTap: () {
setState(() {
snapshot.data[idx].countSelected++;
});
},
),
);
},
);
}
},
);
}
this is what the problem is
尝试这样的事情,创建一个列表
List<ItemModel> listItems = [];
然后,在 initState()
调用 _getItems()
_getItems() async {
var url = Uri.parse(BASEURLLOCAL.apiGetItems);
var data = await http.get(url);
var jsonData = jsonDecode(data.body);
for (var p in jsonData) {
ItemModel item = ItemModel(
p["name"],
p["price"],
p["image"],
// p["PRODUCT_NAME"],
// p["PRICE_SELL"],
// p["FILENAME"],
);
listItems.add(item);
}
}
并删除 FutureBuilder
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
itemCount: listItems.length,
itemBuilder: (BuildContext context, int idx) {
if (_itemCount.length < listItems.length) {
_itemCount.add(0);
}
return Card(
child: ListTile(
leading: Image.network(BASEURLLOCAL.imgItem + listItems[idx].image),
title: Text(listItems[idx].name),
subtitle: Text(listItems[idx].price),
// trailing: _buildTrailing(),
trailing: Text(listItems[idx].countSelected.toString()),
onTap: () {
setState(() {
listItems[idx].countSelected++;
});
},
),
);
},
);
}
这样重建的时候就不会每次都抓取了
我想在尾部增加数字 ListTile
Flutter when ontap ListTile,
但总是 return 到 0?
我正在使用 future builder fyi,
提前致谢
this my model
class ItemModel {
String name;
String price;
String image;
bool isSelected = false;
int countSelected = 0;
ItemModel(
this.name,
this.price,
this.image,
);
}
此方法从本地服务器apijson获取数据
Future<List<ItemModel>> _getItems() async {
List<ItemModel> listItems = [];
var url = Uri.parse(BASEURLLOCAL.apiGetItems);
var data = await http.get(url);
var jsonData = jsonDecode(data.body);
for (var p in jsonData) {
ItemModel item = ItemModel(
p["name"],
p["price"],
p["image"],
// p["PRODUCT_NAME"],
// p["PRICE_SELL"],
// p["FILENAME"],
);
listItems.add(item);
}
return listItems;
}
这是列表视图生成器
@override
Widget build(BuildContext context) {
return FutureBuilder<List<ItemModel>>(
future: _getItems(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext contex, int idx) {
if (_itemCount.length < snapshot.data.length) {
_itemCount.add(0);
}
return Card(
child: ListTile(
leading: Image.network(
BASEURLLOCAL.imgItem + snapshot.data[idx].image),
title: Text(snapshot.data[idx].name),
subtitle: Text(snapshot.data[idx].price),
// trailing: _buildTrailing(),
trailing: Text(snapshot.data[idx].countSelected.toString()),
onTap: () {
setState(() {
snapshot.data[idx].countSelected++;
});
},
),
);
},
);
}
},
);
}
this is what the problem is
尝试这样的事情,创建一个列表
List<ItemModel> listItems = [];
然后,在 initState()
调用 _getItems()
_getItems() async {
var url = Uri.parse(BASEURLLOCAL.apiGetItems);
var data = await http.get(url);
var jsonData = jsonDecode(data.body);
for (var p in jsonData) {
ItemModel item = ItemModel(
p["name"],
p["price"],
p["image"],
// p["PRODUCT_NAME"],
// p["PRICE_SELL"],
// p["FILENAME"],
);
listItems.add(item);
}
}
并删除 FutureBuilder
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
itemCount: listItems.length,
itemBuilder: (BuildContext context, int idx) {
if (_itemCount.length < listItems.length) {
_itemCount.add(0);
}
return Card(
child: ListTile(
leading: Image.network(BASEURLLOCAL.imgItem + listItems[idx].image),
title: Text(listItems[idx].name),
subtitle: Text(listItems[idx].price),
// trailing: _buildTrailing(),
trailing: Text(listItems[idx].countSelected.toString()),
onTap: () {
setState(() {
listItems[idx].countSelected++;
});
},
),
);
},
);
}
这样重建的时候就不会每次都抓取了