'How to get dates by days which have intervals of 7 days each

hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.

so far i tried is

final now = DateTime.now();

for(var d = 1 ; d <= 5 ; d++){
  // Don't know whats next to do here to get the same result below
}

i want result like this

Apr 19, 2022
Apr 26, 2022
May  3, 2022
May 10, 2022
May 17, 2022

can some help me and explain.



Solution 1:[1]

Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.

Therefore, what you can do is:

DateTime now = DateTime.now();

for(var d = 1; d <= 5; d++) {
  ...
  now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}

I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nTerior