by @kodeazy
flutter How to check the position of element in a List dart file?
- In this blog we will discuss on how to identify the position of element in a
List
. - we use
indexOf
function to check the position. - if element is present returns the position of the element if not present returns
-1
. -
Below is the syntax.
List<int> exampleArray = [1, 2, 3, 4]; exampleArray.indexOf(1)
-
Below is the sample code to checck the position of element.
void main() { List<int> exampleArray = [1, 2, 3, 4]; int index = exampleArray.indexOf(1); if (index != -1) { print('Element Position $index'); } else { print('Element is not present'); } }
Output:
0