I’m new to Flutter. I’m confused. If there is any mistake, please correct me.
Here, I’m using Provider and FutureBuilder. When “+” is pressed, it will add to the item in the provider. If the same “+” is pressed, it will add its quantity in the provider.
I hope that when I open this page again, the quantity value will still be there for each corresponding item. So in buildButtonCart
, I added this:
Widget buildButtonCart({required LayananModel value}) {
return Consumer<ItemCollectionProvider>(
builder: (_, val, __) {
return InputQty.int(
...
initVal: val.getJumlah(value: value),
...
onQtyChanged: (_) {
val.add(value: value, jumlah: _);
},
);
},
);
}
But the result is still 0, this image:
https://drive.google.com/file/d/1ntOqxEFfqdKflOhIegeMuwpuLTpFYVo2/view?usp=sharing
Sorry I can’t upload the image, because when uploading, it always fails.
When I search, the result is the same, which is still 0. And, when I go back and open it again, the result is still 0. I’ve checked it by debugging in the provider. The data has been saved.
Am I implementing this wrong?
Here I attach some of the code needed:
PilihLayananPage.dart:
class _PilihLayananPageState extends State<PilihLayananPage> {
bool _match = false;
late Future<List<LayananModel>> _future;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildHeader(),
body: buildLayout(),
);
}
@override
void initState() {
super.initState();
_future = LayananRepository.all(equal: false, limit: 30);
}
AppBar buildHeader() {
return AppBar(
title: Text("Pilih Layanan"),
);
}
Widget buildLayout() {
...
}
Widget buildMatch() {
...
}
FutureBuilder<List<LayananModel>> buildFutureBuilderCollection() {
return FutureBuilder<List<LayananModel>>(
future: _future,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
}
if (snapshot.hasData && snapshot.data!.isEmpty) {
return Center(
child: Text("Data kosong"),
);
}
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
List<LayananModel> daftarLayanan = snapshot.data!;
return ListView.separated(
separatorBuilder: (context, index) => Divider(),
itemBuilder: (context, index) {
LayananModel layanan = daftarLayanan[index];
return ListTile(
title: Text(layanan.nama),
subtitle: Text(layanan.deskripsi),
trailing: buildButtonCart(value: daftarLayanan[index]),
);
},
itemCount: snapshot.data!.length,
);
}
}
return Center(
child: CircularProgressIndicator(),
);
},
);
}
FutureBuilder<List<LayananModel>> buildFutureBuilderSearch() {
return FutureBuilder<List<LayananModel>>(
future: _future,
builder: (context, snapshot) {
return SearchBar(
...
onSubmitted: (value) {
setState(() {
_future = LayananRepository.all(
query: value,
limit: 15,
equal: _match,
);
});
},
);
},
);
}
Widget buildButtonCart({required LayananModel value}) {
return Consumer<ItemCollectionProvider>(
builder: (_, val, __) {
return InputQty.int(
...
initVal: val.getJumlah(value: value),
...
onQtyChanged: (_) {
val.add(value: value, jumlah: _);
},
);
},
);
}
}
ItemProvider.dart
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:londri/models/item_model.dart';
import 'package:londri/models/layanan_model.dart';
class ItemCollectionProvider extends ChangeNotifier {
final List<ItemModel> _daftarItem = [];
UnmodifiableListView<ItemModel> get daftarItem =>
UnmodifiableListView(_daftarItem);
void add({required LayananModel value, required int jumlah}) {
if (_isDuplicate(value: value)) {
_countJumlah(value: value, jumlah: jumlah);
} else {
_daftarItem.add(_getItemObjectFrom(value: value));
}
notifyListeners();
}
ItemModel getItem({required LayananModel value}) {
return _daftarItem[_getIndex(value: value)];
}
int getJumlah({required LayananModel value}) {
try {
return _daftarItem[_getIndex(value: value)].jumlah;
} catch (error) {
return 0;
}
}
ItemModel _getItemObjectFrom({required LayananModel value}) {
return ItemModel(
itemId: 0,
layananId: value.layananId,
terimaId: 0,
nama: value.nama,
deskripsi: value.deskripsi,
jumlah: 0,
);
}
bool _isDuplicate({required LayananModel value}) {
for (final _ in _daftarItem) {
if (_.layananId == value.layananId) {
return true;
}
}
return false;
}
int _getIndex({required LayananModel value}) {
int result =
_daftarItem.indexWhere((val) => val.layananId == value.layananId);
print(result);
return result;
}
void _countJumlah({required LayananModel value, required int jumlah}) {
int index = _getIndex(value: value);
if (_daftarItem[index].jumlah <= 0) {
/// Remove item here
_daftarItem.removeAt(index);
} else {
_daftarItem[index].jumlah = jumlah;
}
}
void removeAll() {
_daftarItem.clear();
}
}