Reputation: 359
I am fetching data from sql lite like this
void _query() async {
//print('cart');
final dbHelper = DatabaseHelper.instance;
allRows = await dbHelper.queryAllRows();
allRows.forEach((row) {
amount += double.parse(row['price']);
items['Items'].add(row);
//print(amount);
//print(row);
});
print(allRows.length);
if (allRows.length == 0) {
showCart = false;
} else {
showCart = true;
}
setState(() {});
}
and I have a List view builder in that I am showing values like this
ListView buildListCartCard() {
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: items['Items'].length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
margin: EdgeInsets.symmetric(vertical: 8.0),
child: Container(
width: double.infinity,
height: 120.0,
padding: EdgeInsets.all(12.0),
child: Row(
children: [
ClipRRect(
borderRadius:
BorderRadius.circular(12.0),
child: Image.network(
items['Items'][index]['image'],
width: 100,
height: 100)),
SizedBox(width: 12.0),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 10.0),
child: Column(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Expanded(
child: Text(
items['Items'][index]
['title'],
textAlign:
TextAlign.start,
maxLines: 3,
overflow: TextOverflow
.ellipsis,
),
),
SizedBox(width: 5.0),
Row(
children: [
GestureDetector(
onTap: () async {
//print('delete');
final dbHelper = DatabaseHelper.instance;
final id = await dbHelper.queryRowCount();
//print(id);
final rowsDeleted = await dbHelper.delete(
id, items['Items'][index]['id']);
print('deleted $rowsDeleted row(s): row $id');
},
child: Icon(
FlutterIcons
.delete_outline_mco,
),
)
],
)
],
),
Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: [
Expanded(
child: Text(
items['Items'][index]
['price'],
),
),
// Counter(),
],
),
],
),
),
)
],
),
),
)
],
);
},
);
}
The issue is when I click I am deleting the items you can see in GestureDetector but its not deleting from ListBuilder I need to refresh page its deleting. I need to know solution for this how its possible to refresh or change state so It can show the updated list.
What i try is i use setState in Gesture detector and call query function again. But issue is in that case its duplicating the list --
Upvotes: 0
Views: 175
Reputation: 1430
After deleting, delete the list. You can recall.
GestureDetector(
onTap: () async {
//print('delete');
final dbHelper = DatabaseHelper.instance;
final id = await dbHelper.queryRowCount();
//print(id);
final rowsDeleted = await dbHelper.delete(
if(rowsDeleted){
setstate(({
items.clear();
_query();
}));
}
},
....
Upvotes: 0