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
EasyCreate a function called createEventInvitation that generates a formatted event invitation using multi-line strings in Dart.
The function should take five parameters:
- A
String eventName - A
String date - A
String time - A
String location - 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);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number