Dart Classes And Objects – Basic Definition

Dart is an object-oriented programming language, and Objects and Classes are an important part of what you’ll be creating Dart and Flutter. if you are not familiar with OOP concepts, I suggest reading an excellent article at the following Address (OOP: Everything you need to know about Object-Oriented Programming).

Here, we’ll have a quick overview of creating classes and objects in Dart. Let,s begin by creating a FlutterToday Class with two fields, name and surname.

class FlutterToday { 
  String name;
  String surname; 
}

you can create an instance of the FlutterToday class from the main method, and set the name and surname as follows.

main() {
  FlutterToday site =  FlutterToday ();
  site .name = "Flutter";
  site .surname = "Today";
  print('${site .name}  ${site .surname}');
}

there are a couple of interesting features in this code that are worth nothing. Name and Surname are both accessible from outside the class, but in Dart, there are no identifiers such as Private or Public.

So, Every property of the class is considered public unless its name is behind with an underscores character(_). in this case, it becomes inaccessible from outside its library (or file).

In the FlutterToday site = FlutterToday (); line, you are creating an instance of FlutterToday class, and the resulting object is contained in the site variable. in Dart, you don’t need to explicitly specify the new keyword, as it is implied. So writing FlutterToday site = new FlutterToday (); would the same.

you will find the omission of the new Keyword extremely common with Dart developers, especially when developing in the flutter framework.


For more Flutter tutorials, Tips, Tricks, Free code, Questions, and Error Solving.

Remember FlutterDecode.com

Leave a Comment