skip to Main Content

I have a List of Pages. The list type is widget. when I tried to find if there is any element match with the HomePage it always return false.

This is my code:

List<Widget> pages = [Home(), SecondPage(), ThirdPage(),SizedBox()]

when I check with the condition pages.contains(Home()) it gives false.

how do I find if the HomePage is currently present in the list or not.So that I can navigate to the correct previous page.

2

Answers


  1. You should try with runtimeType.

    pages.map((e) -> e.runtimeType).contains(Home().runtimeType)

    Reason:
    Objects in flutter are not equal (except primitive data types and String) by default. Hence 2 objects of Home() is not equals. contains works with equality. runtimeType of class will be string so contains will work.

    Login or Signup to reply.
  2. This helps you.

    bool hasHome = pages.any((page) => page is Home);
    

    Iterable any method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search