Ad space (banner)
Lesson 11 / 20

Classes (object orientation)

This lesson covers the basics of writing a class in Dart.

A class bundles data (fields) together with behaviour (methods). Dart is designed around the idea that everything is an object.

The sample code defines class Animal with a name field and a speak() method that returns the sound it makes.

A common early stumble is when fields get initialised. In Dart, a field not initialised in the constructor must either be marked with ? or be given an initial value, under the null-safety rules.

In professional work, almost every screen component (widget) in Flutter is defined as a class, so understanding classes is the foundation of Flutter development.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)