Menu
Coddy logo textTech

Multi-line Strings

Part of the Fundamentals section of Coddy's Dart journey — lesson 39 of 94.

Multi-line strings in Dart allow you to create strings that span multiple lines without needing to use concatenation or escape characters.

Create a multi-line string using triple quotes:

String poem = '''
Roses are red,
Violets are blue,
Dart is awesome,
And so are you!
''';

Print the multi-line string:

print(poem);

After executing the above code, the output will be:

Roses are red,
Violets are blue,
Dart is awesome,
And so are you!
challenge icon

Challenge

Easy

Create a function called createEventInvitation that generates a formatted event invitation using multi-line strings in Dart.

The function should take five parameters:

  1. A String eventName
  2. A String date
  3. A String time
  4. A String location
  5. A String? dresscode (nullable - might not be provided)

The function should return a formatted multi-line string invitation with the following format:

YOU ARE INVITED!
====================
Event: [eventName]
Date: [date]
Time: [time]
Location: [location]
Dress Code: [dresscode or 'Casual attire' if dresscode is null]
====================
We hope to see you there!

Use string interpolation with multi-line strings (triple quotes) to create this formatted output. Make sure to maintain the exact format shown above, including the line of equal signs and the message at the bottom.

Cheat sheet

Create multi-line strings using triple quotes:

String poem = '''
Roses are red,
Violets are blue,
Dart is awesome,
And so are you!
''';

Print multi-line strings:

print(poem);

Use string interpolation within multi-line strings:

String message = '''
Event: $eventName
Date: $date
Location: $location
''';

Try it yourself

import 'dart:io';
import 'dart:convert';

// Function to create a formatted event invitation
String createEventInvitation(String eventName, String date, String time, String location, String? dresscode) {
  // Write your code here to create a multi-line string invitation
  // Remember to use triple quotes (''') for multi-line strings
  // Use string interpolation ($variable) to include the parameters
  // If dresscode is null, use 'Casual attire' instead
}

void main() {
  // Read input as JSON
  String inputJson = stdin.readLineSync()!;
  Map<String, dynamic> eventData = jsonDecode(inputJson);
  
  // Extract values from input
  String eventName = eventData['eventName'];
  String date = eventData['date'];
  String time = eventData['time'];
  String location = eventData['location'];
  String? dresscode = eventData['dresscode']; // This might be null
  
  // Call function and print the result
  String invitation = createEventInvitation(eventName, date, time, location, dresscode);
  print(invitation);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals