3

Ok i think its newbie question I have map with datetime-list, if i do something like this its work perfect:

    Map<DateTime, List> _activities;
   _activities = {
  _currentDay.add(Duration(days: 0)):activityNames.toList(),
  _currentDay.add(Duration(days: 7)):activityNames.toList(),
  _currentDay.add(Duration(days: 14)): activityNames.toList(),
  _currentDay.add(Duration(days: 21)): activityNames.toList(),
  _currentDay.add(Duration(days: 28)): activityNames.toList(),
  }

But i want it like in 'for loop' => wrong example:

  for( var i = 0 ; i <= 28; i=i+7 ) {
_activities[_currentDay.add(Duration(days:i))]=activityNames.toList();

 }

looks good to me but says NoSuchMethedError: The method '[]=' was called null.

1 Answer 1

3

In first code, you are initializing the _activites variable, because of that you don't get error;

_activities = {.....

But in the second one, you don't initialize, so this is what you need:

Map<DateTime, List> _activities = {};
for( var i = 0 ; i <= 28; i=i+7 ) {
_activities[_currentDay.add(Duration(days:i))]=activityNames.toList();

 }

In Dart every object is null by default, so you must initialize it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.