Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre What are Methods in Dart? | List and String
Introduction to Dart
course content

Contenu du cours

Introduction to Dart

Introduction to Dart

1. First Acquaintance with Dart
2. Variables and Data Types in Dart
3. Conditional Statements
4. List and String
5. Loops in Dart

book
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:

dart

main

copy
12345
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:

dart

main

copy
12345
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.

dart

main

copy
12345
void main() { String text = "Hello, world!"; String part = text.substring(0, 5); // Extracts "Hello" print(part); // Outputs: Hello }
How to extract the substring "Programming" from the String str?

How to extract the substring "Programming" from the String str?

Sélectionnez la réponse correcte

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 3
We're sorry to hear that something went wrong. What happened?
some-alt