OneBite.Dev - Coding blog in a bite size

Create An Array Of Number In Dart

Code snippet for how to Create An Array Of Number In Dart with sample and detail explanation

In this article, we will learn how to create an array of numbers in Dart, a simple yet powerful programming language developed by Google. This widely-used language gives you the ability to create an array (known as a list in Dart) in just a few lines of code.

Code snippet for Creating Array of Numbers in Dart

void main() {
  List<int> numberArray = [1, 2, 3, 4, 5];
  print(numberArray);
}

Code Explanation for Creating Array of Numbers in Dart

Let’s break down the code step by step:

  1. void main: This is the main function where the execution of program begins.

  2. List<int> numberArray = [1, 2, 3, 4, 5];: Here, we are declaring a list of integers using the Dart’s List data type. We referred to it as ‘numberArray’. The list contains five elements which are: 1, 2, 3, 4 and 5. Like an array in other programming languages, the list in Dart is an ordered group of items.

  3. print(numberArray);: This line of code will print out the list of numbers that was just created. When this code is run, the output will be: [1, 2, 3, 4, 5].

By using this basic structure, you can create a list of numbers in Dart. You can modify the content of the list as required by your code, adding or removing numbers, and can even create lists of different data types like String or double.

dart