Keep quantity for each item with Provider and FutureBuilder with InputQty

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();
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật