#StackBounty: #flutter #dart merge three models and filter them out by id in Dart?

Bounty: 50

I faced such a problem: I have a Home page where I show all kinds of drinks in the menu and I want to create a page where the user clicks and get a filtered list, where drinks are already divided into hot and cold according to their ID. I know that the method where is used to filter by ID. But my problem is that I have not just two models which have the same id’s in them, but plus another two models through which I have to filter the values and get the already filtered values from the FilterMenu model. I don’t know how to solve this problem correctly. Please, help me, if you can.

Also addition: all models are entitled by each other by their id’s, so DessertId is bond with DrinksId by mv_id and DrinksId is bonded with FilterMenu by sId.

I thought about combining all these models together and then filtering, but I don’t know how to do it correctly. Is it necessary to merge all the models here?

My models:

class DessertsId {
  final int mvId;
  final int mrId;

  DessertsId({this.mvId, this.mrId});
}

class DrinksId{
  final int mvId;
  final int sId;
  DrinksId({this.mvId, this.sId});
}

from this models i want to get all filtrated items through the previous 2

class FilterMenu{
  final String mTitle;
  final int sId;
  FilterMenu({this.mTitle, this.sId});
}

My first page where user taps and go to the filterted Page:

class Home extends StatelessWidget {
   Home({Key key}) : super(key: key);

  @override
  List<CommonMenu> common = <CommonMenu>[
    CommonMenu(commonTitle: 'Hot Drinks', mrId: 2),
    CommonMenu(commonTitle: 'Cold Drinks', mrId: 1),
  ];
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: common.length,
          itemBuilder: (context, index){
        return ListTile(
          title: Text(common[index].commonTitle),
          onTap: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => Menu(

                )));
          },
        );
      }),
    );
  }
}

My second page where should be all filtered items store:

class Menu extends StatefulWidget {
  final int sId;

  const Menu({Key key, this.sId}) : super(key: key);

  @override
  _MenuState createState() => _MenuState();
}

class _MenuState extends State<Menu> {
  List<DessertsId> desert = <DessertsId>[
    DessertsId(mvId: 1, mrId: 1),
    DessertsId(mvId: 2, mrId: 2),
    DessertsId(mvId: 1, mrId: 1),
    DessertsId(mvId: 2, mrId: 2),
    DessertsId(mvId: 2, mrId: 2),
  ];

  List<DrinksId> drinks = <DrinksId>[
    DrinksId(mvId: 1, sId: 1),
    DrinksId(mvId: 2, sId: 2),
    DrinksId(mvId: 1, sId: 1),
    DrinksId(mvId: 2, sId: 2),
    DrinksId(mvId: 1, sId: 1),
    DrinksId(mvId: 2, sId: 2),
  ];

  List<FilterMenu> filterMenu = <FilterMenu>[
    FilterMenu(mTitle: 'capuchino', sId: 1),
    FilterMenu(mTitle: 'americano', sId: 2),
    FilterMenu(mTitle: 'latte', sId: 1),
    FilterMenu(mTitle: 'tonic', sId: 2),
    FilterMenu(mTitle: 'ice latte', sId: 1),
    FilterMenu(mTitle: 'frapuchino', sId: 2),
  ];

  @override
  Widget build(BuildContext context)  {
    return Scaffold(
        appBar: AppBar(),
        body: ListView.builder(
          itemCount: filterMenu.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(filterMenu[index].mTitle),
            );
          },
        ));
  }
}

This is the code I try to filtrate and combined models:

Iterable<CommonItems> merged(List <DessertsId> desert,
        List <DrinksId> drinks, List <FilterMenu> filterMenu) sync* {
      for (var i = 0; i < filterMenu.length; i++) {
        var desertes = desert.where((element) => element.mrId == filterMenu[i].sId).toList();
        var drink = drinks.where((element) => element.sId == filterMenu[i].sId).toList();
        yield CommonItems(
          desert: desertes,
          drinks: drink,
          mrId: filterMenu[i].sId,
          sId: filterMenu[i].sId
        );
      }
    }
    List<CommonItems> common = merged(desert, drinks, filterMenu).toList();

class CommonItems{
  final int sId;
  final int mrId;
 final List<DessertsId> desert;
  final List<DrinksId> drinks;
  List<FilterMenu> filterMenu;
  final List <CommonMenu> menu;

  CommonItems({this.desert, this.drinks, this.menu, this.sId, this.mrId});
}

The screenshots:

first page

second page with filtered items


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.