{

How to create a sequence number in a list in Dart or Flutter | Dart By Example


Generate a sequence of numbers in A List  Dart or Flutter  example

Sometimes, We need to generate a fixed list with a sequence of numbers from 0 to N. This article, Shows multiple ways to create a list of 0 to n numbers.

How to create a sequence of numbers list for a given number in Dart or Flutter

There are multiple ways we can create a sequence of numbers in a list

  • use list generate function

generate() function in a list creates a list with values generated with a given function.

Syntax:

List generate(length, Function,{growable})

The list is created with a length attribute. Function is a generator function logic to have the values in a list growable: false means List is fixed size.

Here is an example to create a list of sequence numbers from 0 to N.

void main() {
  var sequenceNumbers = new List<int>.generate(20, (k) => k + 1);
  print(sequenceNumbers);
}

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  • use Collections for collection for in dart used to creeat a list with manipulation of indexed values.
void main() {
  var numberList = [for (var i = 1; i <= 20; i++) i];
  print(numberList);
}

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Conclusion

To summarize, It is easy to create a sequence of numbers in a list with list generate function and collection for with examples

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example