Dart for Java Devs #1

Dart for Java Devs #1

hackernoon.com hackernoon.com1 year ago in#Dev Love56

@roboboyKartikey Rai helloo break bitches Let’s jump right in! Above the  main()  function, add a  Bicycle  class with three instance variables. Also remove the contents from  main() , as shown in the following code snippet: class Bicycle { int cadence; int speed; int gear; } void main() { } Dart’s main method is named  main() . In Dart, you can define code outside of classes. Variables, functions, getters, and setters can all live outside of classes. The original Java example declares private instance variables using the private tag, which Dart doesn’t use. “Neither main() nor Bicycle is declared as public, because all identifiers are public by default. Dart doesn’t have keywords for public, private, or protected. Dart uses two-character indentation by convention instead of four. Add the following constructor to the Bicycle class: Bicycle(this.cadence, this.speed, this.gear); This constructor has no body, which is valid in Dart. If you forget the semicolon ( ; ) at the end of a no-body constructor, Dart displays the following error: “A function body must be provided. “Using  this  in a constructor’s parameter list is a handy shortcut for assigning values to instance variables. The code above is equivalent to the following: Bicycle(int cadence, int speed, int gear) { this.cadence = cadence; this.speed = speed; this.gear = gear; } Instantiate and print a Bicycle instance void main() { var bike = Bicycle(2, 0, 1); print(bike); } If you know that a variable’s value won’t change, then you can use final instead of var. Run it. The following output comes: Improve the output While the output “Instance of ‘Bicycle’” is correct, it’s not very informative. All Dart classes have a  toString()  method that you can override to provide more useful output. @override String toString() =’Bicycle: $speed mph’; The  @override  annotation tells the analyzer that you are intentionally overriding a member. The analyzer raises an error if you failed to properly perform the override. Dart supports single or double quotes when specifying strings. Use string interpolation to put the value of an expression inside a string literal:  ${expression} . If the expression is an identifier, you can skip the braces:  $variableName . Shorten one-line functions or methods using fat arrow ( =) notation. You should see the following output: Add a read-only variable The original Java example defines  speed  as a read-only variable—it declares it as private and provides only a getter. Next, you’ll provide the same functionality in Dart. To mark a Dart identifier as private to its library, start its name with an underscore ( _ ). You can convert  speed  to read-only by changing its name and adding a getter.  In the Bicycle constructor, remove the speed parameter: Bicycle(this.cadence, this.gear); In  main() , remove the second ( speed ) parameter from the call to the  Bicycle  constructor: var bike = Bicycle(2, 1); Change the remaining occurrences of  speed  to  _speed . (Two places) Add the following getter to the  Bicycle  class:  Observations Uninitialized variables (even numbers) have the value  null .  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply