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.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources