Kursinhalt
Introduction to Dart
Introduction to Dart
1. First Acquaintance with Dart
2. Variables and Data Types in Dart
What are Methods in Dart?
Imagine you have a list of numbers and need to add a new number or remove an existing one. In Dart, you use methods for this.
What is a Method?
Methods help manage data and perform operations on objects. Calling a method looks like this:
Dart has countless methods for working with different data types, but let's look at some of the most popular ones—starting with lists and then moving on to strings.
Using Methods with Lists
The add()
method appends an element to the end of a list:
main
void main() { List<int> numbers = [1, 2, 3]; numbers.add(4); // Adds 4 to the list print(numbers); // Outputs: [1, 2, 3, 4] }
The remove()
method deletes a specific element:
main
void main() { List<int> numbers = [1, 2, 3, 4]; numbers.remove(2); // Removes 2 from the list print(numbers); // Outputs: [1, 3, 4] }
Using Methods with Strings
The substring()
method extracts part of a string. It takes two arguments: the start and end indexes.
main
void main() { String text = "Hello, world!"; String part = text.substring(0, 5); // Extracts "Hello" print(part); // Outputs: Hello }
War alles klar?
Danke für Ihr Feedback!
Abschnitt 4. Kapitel 3