Search code examples
flutterlistdartnavigationwidget

How to find the page is currently in the list or not?


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.


Solution

  • 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.