Home > Back-end >  Print 2 elements in a list
Print 2 elements in a list

Time:01-07

I am trying to add all elements to a list, Here is my code the code below will run in dart pad.

void main() {
  mydates();
}

class Event {
  const Event(this.date, this.title, this.description);

  final DateTime date;
  final String title;
  final String description;

  @override
  String toString() => title;
}

List<Event> eventList = [
  Event(DateTime(2021, 1, 1), "Buy milk",
      "Lorem ipsum dolor sit amet, consectetur adipiscingelit. Pellentesque sem"),
  Event(DateTime(2021, 1, 1), "Go to gym",
      "Duis congue enim ut justo interdum, id porta turpisvarius"),
  Event(DateTime(2021, 1, 2), "Running",
      "Fusce in varius lorem. Praesent accumsan metus at semfaucibus"),
];

//Function
mydates() {
  var eventDayMap = <DateTime, List<Event>>{};
  for (var event in eventList) {
    print(event.date); // Prints each element in the List<Event>
    print(event.title);
    print(event.description);

    // Adding them to the eventDayMap
    // Creates a new list
    // Adds only the date and title
    (eventDayMap[event.date] ??= []).add(event);
  }
  print(eventDayMap);
  //print(eventDayMap.runtimeType);
  return eventDayMap;
}

What is returned is this

{2021-01-01 00:00:00.000: [Buy milk, Go to gym], 2021-01-02 00:00:00.000: [Running]}

What I'm trying to achieve is the {date, title, description} and there can be many title and descriptions for one date.

{2021-01-01 00:00:00.000: [Buy milk, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sem ],
[Go to gym, Duis congue enim ut justo interdum, id porta turpis varius], 
2021-01-02 00:00:00.000: [Running, Fusce in varius lorem. Praesent accumsan metus at sem faucibus]}

So I can have a title and a description returned.

Thanks for any guidance.

CodePudding user response:

I think I worked it out after a long time trying to do it my self it comes to me after posting it here.

I needed to return a List instead of a single event.

void main() {
  mydates();
}

class Event {
  const Event(this.date, this.title /*, this.description*/);

  final DateTime date;
  final List title;
  //final String description;

  //@override
  //String toString() => title;
}

List<Event> eventList = [
  Event(DateTime(2021, 1, 1), [
    "Buy milk",
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sem"
  ]),
  Event(DateTime(2021, 1, 1), [
    "Go to gym",
    "Duis congue enim ut justo interdum, id porta turpis varius"
  ]),
  Event(DateTime(2021, 1, 2), [
    "Running",
    "Fusce in varius lorem. Praesent accumsan metus at sem faucibus"
  ]),
];

//Function
mydates() {
  var eventDayMap = <DateTime, List<Event>>{};

  for (var event in eventList) {
    print(event.date); // Prints each element in the List<Event>
    print(event.title);
    //print(event.description);

    // Adding them to the eventDayMap
    // Creates a new list
    // Adds only the date and title
    (eventDayMap[event.date] ??= []).add(event);
  }
  print(eventDayMap);
  //print(eventDayMap.runtimeType);
  return eventDayMap;
}

Now it returns

2021-01-01 00:00:00.000
[Buy milk, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sem]
2021-01-01 00:00:00.000
[Go to gym, Duis congue enim ut justo interdum, id porta turpis varius]
2021-01-02 00:00:00.000
[Running, Fusce in varius lorem. Praesent accumsan metus at sem faucibus]

Not exactly what I was looking for but it might be a better solution.

CodePudding user response:

What your problem is depends on what your goal is.

If you want to create a valid JSON map, you will want to represent each element by a list. That doesn't appear to be the case since your keys are DateTime objects, not strings.

If you just want to be able to print the events, for debugging, then your problem is the String toString() => title; declaration.

Your map is fine, it maps from dates to Event objects. That's what you'll want.

Then you try to print that map, which calls toString on each value, which only returns the title string. That only matters if you actually print the map, which you shouldn't be doing (except for debugging).

If you change toString to

String toString() => "[$title, $description]";

you'll likely get what it appears you're looking for.

  •  Tags:  
  • Related