Home > database >  Flutter split list and calculate with individual values
Flutter split list and calculate with individual values

Time:09-06

I am working on a grocery shopping list. A user shall be able to add recipe ingredients on click to the list. Let's assume we have two recipes with the following ingredients:

Recipe 1: 3 apples, 2 bananas, 50 g yogurt

Recipe 2: 2 apples, 1 bread, 50 g cream cheese

Currently my code is this where I get the list items:

@override
   Widget build(BuildContext context){
    return Scaffold(
       backgroundColor: Colors.grey[900],
         body:  ValueListenableBuilder(
          valueListenable:  Hive.box('shopping').listenable(),
         
          builder: (context, __box, _)  {
            var __box = Hive.box('shopping');
            final shoppingTransactions = __box.values.toList();
            print('shopping Transactions list is ${shoppingTransactions}');
            for (var i=0; i<shoppingTransactions.length; i  ) {
            List flatList = shoppingTransactions.expand((i) => i).toList();

Output from flatList is:

flutter: final individual list is [3 apples, 2 bananas, 50 g yogurt, 2 apples, 1 bread, 50 g cream cheese]

In a listview I want to display the following:

5 apples

2 bananas

50g yogurt

1 bread

50g cream cheese

How do I split the list items to be able to group ingredients and sum them up to display them in the list view?

CodePudding user response:

you can use ListView.builder like this :

ListView.builder(
  itemCount: flatList.length,
  itemBuilder: (context, index) {
return ListTile(
  title: Text(flatList[index]),
);
 },
  ),

this will simply make a listview of all elements of your list

Hope this helps

CodePudding user response:

first create model class like this:

class Ingredient {
  final int count;
  final String name;

  const Ingredient({
    required this.count,
    required this.name,
  });

  factory Ingredient.fromString(String str) {
    var strList = str.split(' ');
    int _count = int.parse(strList[0]);
    strList.removeAt(0);
    String _name = '';
    for (var element in strList) {
      _name = _name   element   ' ';
    }
    return Ingredient(
      count: _count,
      name: _name,
    );
  }
}

then use it like this:

    List<Ingredient> ingredients = flatList.map((e) => Ingredient.fromString(e)).toList();
    List<Ingredient> finalIngredients = [];
    for (var item in ingredients) {
      var index = 0;
      bool addedBefore = false;
      for (var element in finalIngredients) {
        if (element.name == item.name) {
          addedBefore = true;
          break;
        } else {
          index  ;
        }
      }
      if (addedBefore) {
        finalIngredients[index] = Ingredient(
            count: finalIngredients[index].count   item.count,
            name: finalIngredients[index].name);
      } else {
        finalIngredients.add(item);
      }
    }
    print("apples=${finalIngredients[0].count}"); //apples=5

then use finalIngredients to show your list in listview.

  • Related