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
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.This helps you.
Iterable any method