How to use the in keyword in Dart

Overview

In Dart, the in keyword is used in the for-in loop. When we iterate through an iterable, such as a list or a set, the for-in loop comes in handy.

Syntax

for (variablename in iterable){  
   // some code to execute  
}

Code

The following code shows how to use the in keyword in Dart.

void main() {
// create list
var lang = ['D','a','r','t'];
// iterate through the list
// using for-in loop
for (var x in lang) {
print(lang);
}
}

Explanation

  • Lines 1–9: We create the main() function.
  • Line 3: In main(), we declare a list called lang.
  • Lines 6–8: We use the for-in loop to iterate through the list lang and display the items in the list.

Free Resources