mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-09 16:26:53 +02:00
prefer using 3 slash for code comments
This commit is contained in:
@@ -3,27 +3,28 @@ language: dart
|
|||||||
filename: learndart.dart
|
filename: learndart.dart
|
||||||
contributors:
|
contributors:
|
||||||
- ["Joao Pedrosa", "https://github.com/jpedrosa/"]
|
- ["Joao Pedrosa", "https://github.com/jpedrosa/"]
|
||||||
|
- ["Vince Ramces Oliveros", "https://github.com/ram231"]
|
||||||
---
|
---
|
||||||
|
|
||||||
Dart is a newcomer into the realm of programming languages.
|
Dart is a newcomer into the realm of programming languages.
|
||||||
It borrows a lot from other mainstream languages, having as a goal not to deviate too much from
|
It borrows a lot from other mainstream languages, having as a goal not to deviate too much from
|
||||||
its JavaScript sibling. Like JavaScript, Dart aims for great browser integration.
|
~~its JavaScript sibling. Like JavaScript, Dart aims for great browser integration.~~
|
||||||
|
|
||||||
Dart's most controversial feature must be its Optional Typing.
|
Dart's most controversial feature must be its ~~Optional Typing~~ Static Type safety and [Sound Type checks](https://dart.dev/guides/language/sound-dart).
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
import "dart:collection";
|
import "dart:collection";
|
||||||
import "dart:math" as DM;
|
import "dart:math" as math;
|
||||||
|
|
||||||
// Welcome to Learn Dart in 15 minutes. http://www.dartlang.org/
|
/// Welcome to Learn Dart in 15 minutes. http://dart.dev/
|
||||||
// This is an executable tutorial. You can run it with Dart or on
|
/// This is an executable tutorial. You can run it with Dart or on
|
||||||
// the Try Dart! site if you copy/paste it there. http://try.dartlang.org/
|
/// the Try Dart! site if you copy/paste it there. http://dartpad.dev/
|
||||||
|
/// You can also run Flutter in DartPad by click the `< > New Pad ` and choose Flutter
|
||||||
// Function declaration and method declaration look the same. Function
|
/// Function declaration and method declaration look the same. Function
|
||||||
// declarations can be nested. The declaration takes the form of
|
/// declarations can be nested. The declaration takes the form of
|
||||||
// name() {} or name() => singleLineExpression;
|
/// name() {} or name() => singleLineExpression;
|
||||||
// The fat arrow function declaration has an implicit return for the result of
|
/// The fat arrow function declaration can be an implicit or explicit return for the result of
|
||||||
// the expression.
|
/// the expression.
|
||||||
example1() {
|
example1() {
|
||||||
nested1() {
|
nested1() {
|
||||||
nested2() => print("Example1 nested 1 nested 2");
|
nested2() => print("Example1 nested 1 nested 2");
|
||||||
@@ -32,29 +33,30 @@ example1() {
|
|||||||
nested1();
|
nested1();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anonymous functions don't include a name.
|
/// Anonymous functions don't include a name.
|
||||||
example2() {
|
example2() {
|
||||||
nested1(fn) {
|
//// Explicit return type.
|
||||||
|
nested1(Function<void> fn) {
|
||||||
fn();
|
fn();
|
||||||
}
|
}
|
||||||
nested1(() => print("Example2 nested 1"));
|
nested1(() => print("Example2 nested 1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a function parameter is declared, the declaration can include the
|
/// When a function parameter is declared, the declaration can include the
|
||||||
// number of parameters the function takes by specifying the names of the
|
/// number of parameters the function takes by specifying the names of the
|
||||||
// parameters it takes.
|
/// parameters it takes.
|
||||||
example3() {
|
example3() {
|
||||||
planA(fn(informSomething)) {
|
planA(fn(informSomething)) {
|
||||||
fn("Example3 plan A");
|
fn("Example3 plan A");
|
||||||
}
|
}
|
||||||
planB(fn) { // Or don't declare number of parameters.
|
planB(fn) { /// Or don't declare number of parameters.
|
||||||
fn("Example3 plan B");
|
fn("Example3 plan B");
|
||||||
}
|
}
|
||||||
planA((s) => print(s));
|
planA((s) => print(s));
|
||||||
planB((s) => print(s));
|
planB((s) => print(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions have closure access to outer variables.
|
/// Functions have closure access to outer variables.
|
||||||
var example4Something = "Example4 nested 1";
|
var example4Something = "Example4 nested 1";
|
||||||
example4() {
|
example4() {
|
||||||
nested1(fn(informSomething)) {
|
nested1(fn(informSomething)) {
|
||||||
@@ -63,8 +65,8 @@ example4() {
|
|||||||
nested1((s) => print(s));
|
nested1((s) => print(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class declaration with a sayIt method, which also has closure access
|
/// Class declaration with a sayIt method, which also has closure access
|
||||||
// to the outer variable as though it were a function as seen before.
|
/// to the outer variable as though it were a function as seen before.
|
||||||
var example5method = "Example5 sayIt";
|
var example5method = "Example5 sayIt";
|
||||||
class Example5Class {
|
class Example5Class {
|
||||||
sayIt() {
|
sayIt() {
|
||||||
@@ -72,14 +74,14 @@ class Example5Class {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
example5() {
|
example5() {
|
||||||
// Create an anonymous instance of the Example5Class and call the sayIt
|
/// Create an anonymous instance of the Example5Class and call the sayIt
|
||||||
// method on it.
|
/// method on it.
|
||||||
new Example5Class().sayIt();
|
new Example5Class().sayIt();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class declaration takes the form of class name { [classBody] }.
|
/// Class declaration takes the form of class name { [classBody] }.
|
||||||
// Where classBody can include instance methods and variables, but also
|
/// Where classBody can include instance methods and variables, but also
|
||||||
// class methods and variables.
|
/// class methods and variables.
|
||||||
class Example6Class {
|
class Example6Class {
|
||||||
var instanceVariable = "Example6 instance variable";
|
var instanceVariable = "Example6 instance variable";
|
||||||
sayIt() {
|
sayIt() {
|
||||||
@@ -90,7 +92,7 @@ example6() {
|
|||||||
new Example6Class().sayIt();
|
new Example6Class().sayIt();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class methods and variables are declared with "static" terms.
|
/// Class methods and variables are declared with "static" terms.
|
||||||
class Example7Class {
|
class Example7Class {
|
||||||
static var classVariable = "Example7 class variable";
|
static var classVariable = "Example7 class variable";
|
||||||
static sayItFromClass() {
|
static sayItFromClass() {
|
||||||
@@ -105,11 +107,11 @@ example7() {
|
|||||||
new Example7Class().sayItFromInstance();
|
new Example7Class().sayItFromInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Literals are great, but there's a restriction for what literals can be
|
/// Literals are great, but there's a restriction for what literals can be
|
||||||
// outside of function/method bodies. Literals on the outer scope of class
|
/// outside of function/method bodies. Literals on the outer scope of class
|
||||||
// or outside of class have to be constant. Strings and numbers are constant
|
/// or outside of class have to be constant. Strings and numbers are constant
|
||||||
// by default. But arrays and maps are not. They can be made constant by
|
/// by default. But arrays and maps are not. They can be made constant by
|
||||||
// declaring them "const".
|
/// declaring them "const".
|
||||||
var example8Array = const ["Example8 const array"],
|
var example8Array = const ["Example8 const array"],
|
||||||
example8Map = const {"someKey": "Example8 const map"};
|
example8Map = const {"someKey": "Example8 const map"};
|
||||||
example8() {
|
example8() {
|
||||||
@@ -117,9 +119,9 @@ example8() {
|
|||||||
print(example8Map["someKey"]);
|
print(example8Map["someKey"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loops in Dart take the form of standard for () {} or while () {} loops,
|
/// Loops in Dart take the form of standard for () {} or while () {} loops,
|
||||||
// slightly more modern for (.. in ..) {}, or functional callbacks with many
|
/// slightly more modern for (.. in ..) {}, or functional callbacks with many
|
||||||
// supported features, starting with forEach.
|
/// supported features, starting with forEach.
|
||||||
var example9Array = const ["a", "b"];
|
var example9Array = const ["a", "b"];
|
||||||
example9() {
|
example9() {
|
||||||
for (var i = 0; i < example9Array.length; i++) {
|
for (var i = 0; i < example9Array.length; i++) {
|
||||||
@@ -136,7 +138,7 @@ example9() {
|
|||||||
example9Array.forEach((e) => print("Example9 forEach loop '${e}'"));
|
example9Array.forEach((e) => print("Example9 forEach loop '${e}'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// To loop over the characters of a string or to extract a substring.
|
/// To loop over the characters of a string or to extract a substring.
|
||||||
var example10String = "ab";
|
var example10String = "ab";
|
||||||
example10() {
|
example10() {
|
||||||
for (var i = 0; i < example10String.length; i++) {
|
for (var i = 0; i < example10String.length; i++) {
|
||||||
@@ -147,14 +149,14 @@ example10() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int and double are the two supported number formats.
|
/// Int and double are the two supported number formats.
|
||||||
example11() {
|
example11() {
|
||||||
var i = 1 + 320, d = 3.2 + 0.01;
|
var i = 1 + 320, d = 3.2 + 0.01;
|
||||||
print("Example11 int ${i}");
|
print("Example11 int ${i}");
|
||||||
print("Example11 double ${d}");
|
print("Example11 double ${d}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// DateTime provides date/time arithmetic.
|
/// DateTime provides date/time arithmetic.
|
||||||
example12() {
|
example12() {
|
||||||
var now = new DateTime.now();
|
var now = new DateTime.now();
|
||||||
print("Example12 now '${now}'");
|
print("Example12 now '${now}'");
|
||||||
@@ -162,7 +164,7 @@ example12() {
|
|||||||
print("Example12 tomorrow '${now}'");
|
print("Example12 tomorrow '${now}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular expressions are supported.
|
/// Regular expressions are supported.
|
||||||
example13() {
|
example13() {
|
||||||
var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
|
var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$");
|
||||||
match(s) {
|
match(s) {
|
||||||
@@ -176,7 +178,7 @@ example13() {
|
|||||||
match(s2);
|
match(s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Boolean expressions support implicit conversions and dynamic type
|
/// Boolean expressions support implicit conversions and dynamic type
|
||||||
example14() {
|
example14() {
|
||||||
var a = true;
|
var a = true;
|
||||||
if (a) {
|
if (a) {
|
||||||
@@ -186,11 +188,11 @@ example14() {
|
|||||||
if (a) {
|
if (a) {
|
||||||
print("true, a is $a");
|
print("true, a is $a");
|
||||||
} else {
|
} else {
|
||||||
print("false, a is $a"); // runs here
|
print("false, a is $a"); /// runs here
|
||||||
}
|
}
|
||||||
|
|
||||||
// dynamic typed null can be convert to bool
|
/// dynamic typed null can be convert to bool
|
||||||
var b;// b is dynamic type
|
var b;/// b is dynamic type
|
||||||
b = "abc";
|
b = "abc";
|
||||||
try {
|
try {
|
||||||
if (b) {
|
if (b) {
|
||||||
@@ -199,35 +201,35 @@ example14() {
|
|||||||
print("false, b is $b");
|
print("false, b is $b");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("error, b is $b"); // this could be run but got error
|
print("error, b is $b"); /// this could be run but got error
|
||||||
}
|
}
|
||||||
b = null;
|
b = null;
|
||||||
if (b) {
|
if (b) {
|
||||||
print("true, b is $b");
|
print("true, b is $b");
|
||||||
} else {
|
} else {
|
||||||
print("false, b is $b"); // runs here
|
print("false, b is $b"); /// runs here
|
||||||
}
|
}
|
||||||
|
|
||||||
// statically typed null can not be convert to bool
|
/// statically typed null can not be convert to bool
|
||||||
var c = "abc";
|
var c = "abc";
|
||||||
c = null;
|
c = null;
|
||||||
// complie failed
|
/// complie failed
|
||||||
// if (c) {
|
/// if (c) {
|
||||||
// print("true, c is $c");
|
/// print("true, c is $c");
|
||||||
// } else {
|
/// } else {
|
||||||
// print("false, c is $c");
|
/// print("false, c is $c");
|
||||||
// }
|
/// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// try/catch/finally and throw are used for exception handling.
|
/// try/catch/finally and throw are used for exception handling.
|
||||||
// throw takes any object as parameter;
|
/// throw takes any object as parameter;
|
||||||
example15() {
|
example15() {
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
throw "Some unexpected error.";
|
throw "Some unexpected error.";
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Example15 an exception: '${e}'");
|
print("Example15 an exception: '${e}'");
|
||||||
throw e; // Re-throw
|
throw e; /// Re-throw
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Example15 catch exception being re-thrown: '${e}'");
|
print("Example15 catch exception being re-thrown: '${e}'");
|
||||||
@@ -236,8 +238,8 @@ example15() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// To be efficient when creating a long string dynamically, use
|
/// To be efficient when creating a long string dynamically, use
|
||||||
// StringBuffer. Or you could join a string array.
|
/// StringBuffer. Or you could join a string array.
|
||||||
example16() {
|
example16() {
|
||||||
var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
|
var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e;
|
||||||
for (e in a) { sb.write(e); }
|
for (e in a) { sb.write(e); }
|
||||||
@@ -246,8 +248,8 @@ example16() {
|
|||||||
print("Example16 join string array '${a.join()}'");
|
print("Example16 join string array '${a.join()}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strings can be concatenated by just having string literals next to
|
/// Strings can be concatenated by just having string literals next to
|
||||||
// one another with no further operator needed.
|
/// one another with no further operator needed.
|
||||||
example17() {
|
example17() {
|
||||||
print("Example17 "
|
print("Example17 "
|
||||||
"concatenate "
|
"concatenate "
|
||||||
@@ -255,44 +257,44 @@ example17() {
|
|||||||
"just like that");
|
"just like that");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strings have single-quote or double-quote for delimiters with no
|
/// Strings have single-quote or double-quote for delimiters with no
|
||||||
// actual difference between the two. The given flexibility can be good
|
/// actual difference between the two. The given flexibility can be good
|
||||||
// to avoid the need to escape content that matches the delimiter being
|
/// to avoid the need to escape content that matches the delimiter being
|
||||||
// used. For example, double-quotes of HTML attributes if the string
|
/// used. For example, double-quotes of HTML attributes if the string
|
||||||
// contains HTML content.
|
/// contains HTML content.
|
||||||
example18() {
|
example18() {
|
||||||
print('Example18 <a href="etc">'
|
print('Example18 <a href="etc">'
|
||||||
"Don't can't I'm Etc"
|
"Don't can't I'm Etc"
|
||||||
'</a>');
|
'</a>');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strings with triple single-quotes or triple double-quotes span
|
/// Strings with triple single-quotes or triple double-quotes span
|
||||||
// multiple lines and include line delimiters.
|
/// multiple lines and include line delimiters.
|
||||||
example19() {
|
example19() {
|
||||||
print('''Example19 <a href="etc">
|
print('''Example19 <a href="etc">
|
||||||
Example19 Don't can't I'm Etc
|
Example19 Don't can't I'm Etc
|
||||||
Example19 </a>''');
|
Example19 </a>''');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strings have the nice interpolation feature with the $ character.
|
/// Strings have the nice interpolation feature with the $ character.
|
||||||
// With $ { [expression] }, the return of the expression is interpolated.
|
/// With $ { [expression] }, the return of the expression is interpolated.
|
||||||
// $ followed by a variable name interpolates the content of that variable.
|
/// $ followed by a variable name interpolates the content of that variable.
|
||||||
// $ can be escaped like so \$ to just add it to the string instead.
|
/// $ can be escaped like so \$ to just add it to the string instead.
|
||||||
example20() {
|
example20() {
|
||||||
var s1 = "'\${s}'", s2 = "'\$s'";
|
var s1 = "'\${s}'", s2 = "'\$s'";
|
||||||
print("Example20 \$ interpolation ${s1} or $s2 works.");
|
print("Example20 \$ interpolation ${s1} or $s2 works.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional types allow for the annotation of APIs and come to the aid of
|
/// Optional types allow for the annotation of APIs and come to the aid of
|
||||||
// IDEs so the IDEs can better refactor, auto-complete and check for
|
/// IDEs so the IDEs can better refactor, auto-complete and check for
|
||||||
// errors. So far we haven't declared any types and the programs have
|
/// errors. So far we haven't declared any types and the programs have
|
||||||
// worked just fine. In fact, types are disregarded during runtime.
|
/// worked just fine. In fact, types are disregarded during runtime.
|
||||||
// Types can even be wrong and the program will still be given the
|
/// Types can even be wrong and the program will still be given the
|
||||||
// benefit of the doubt and be run as though the types didn't matter.
|
/// benefit of the doubt and be run as though the types didn't matter.
|
||||||
// There's a runtime parameter that checks for type errors which is
|
/// There's a runtime parameter that checks for type errors which is
|
||||||
// the checked mode, which is said to be useful during development time,
|
/// the checked mode, which is said to be useful during development time,
|
||||||
// but which is also slower because of the extra checking and is thus
|
/// but which is also slower because of the extra checking and is thus
|
||||||
// avoided during deployment runtime.
|
/// avoided during deployment runtime.
|
||||||
class Example21 {
|
class Example21 {
|
||||||
List<String> _names;
|
List<String> _names;
|
||||||
Example21() {
|
Example21() {
|
||||||
@@ -315,7 +317,7 @@ void example21() {
|
|||||||
print("Example21 names '${o.names}' and length '${o.length}'");
|
print("Example21 names '${o.names}' and length '${o.length}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class inheritance takes the form of class name extends AnotherClassName {}.
|
/// Class inheritance takes the form of class name extends AnotherClassName {}.
|
||||||
class Example22A {
|
class Example22A {
|
||||||
var _name = "Some Name!";
|
var _name = "Some Name!";
|
||||||
get name => _name;
|
get name => _name;
|
||||||
@@ -326,13 +328,13 @@ example22() {
|
|||||||
print("Example22 class inheritance '${o.name}'");
|
print("Example22 class inheritance '${o.name}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class mixin is also available, and takes the form of
|
/// Class mixin is also available, and takes the form of
|
||||||
// class name extends SomeClass with AnotherClassName {}.
|
/// class name extends SomeClass with AnotherClassName {}.
|
||||||
// It's necessary to extend some class to be able to mixin another one.
|
/// It's necessary to extend some class to be able to mixin another one.
|
||||||
// The template class of mixin cannot at the moment have a constructor.
|
/// The template class of mixin cannot at the moment have a constructor.
|
||||||
// Mixin is mostly used to share methods with distant classes, so the
|
/// Mixin is mostly used to share methods with distant classes, so the
|
||||||
// single inheritance doesn't get in the way of reusable code.
|
/// single inheritance doesn't get in the way of reusable code.
|
||||||
// Mixins follow the "with" statement during the class declaration.
|
/// Mixins follow the "with" statement during the class declaration.
|
||||||
class Example23A {}
|
class Example23A {}
|
||||||
class Example23Utils {
|
class Example23Utils {
|
||||||
addTwo(n1, n2) {
|
addTwo(n1, n2) {
|
||||||
@@ -351,10 +353,10 @@ example23() {
|
|||||||
print("Example23 addTwo(1, 2) results in '${r2}'");
|
print("Example23 addTwo(1, 2) results in '${r2}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Class constructor method uses the same name of the class and
|
/// The Class constructor method uses the same name of the class and
|
||||||
// takes the form of SomeClass() : super() {}, where the ": super()"
|
/// takes the form of SomeClass() : super() {}, where the ": super()"
|
||||||
// part is optional and it's used to delegate constant parameters to the
|
/// part is optional and it's used to delegate constant parameters to the
|
||||||
// super-parent's constructor.
|
/// super-parent's constructor.
|
||||||
class Example24A {
|
class Example24A {
|
||||||
var _value;
|
var _value;
|
||||||
Example24A({value: "someValue"}) {
|
Example24A({value: "someValue"}) {
|
||||||
@@ -372,9 +374,9 @@ example24() {
|
|||||||
print("Example24 calling super during constructor '${o2.value}'");
|
print("Example24 calling super during constructor '${o2.value}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// There's a shortcut to set constructor parameters in case of simpler classes.
|
/// There's a shortcut to set constructor parameters in case of simpler classes.
|
||||||
// Just use the this.parameterName prefix and it will set the parameter on
|
/// Just use the this.parameterName prefix and it will set the parameter on
|
||||||
// an instance variable of same name.
|
/// an instance variable of same name.
|
||||||
class Example25 {
|
class Example25 {
|
||||||
var value, anotherValue;
|
var value, anotherValue;
|
||||||
Example25({this.value, this.anotherValue});
|
Example25({this.value, this.anotherValue});
|
||||||
@@ -385,9 +387,9 @@ example25() {
|
|||||||
"'${o.anotherValue}'");
|
"'${o.anotherValue}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Named parameters are available when declared between {}.
|
/// Named parameters are available when declared between {}.
|
||||||
// Parameter order can be optional when declared between {}.
|
/// Parameter order can be optional when declared between {}.
|
||||||
// Parameters can be made optional when declared between [].
|
/// Parameters can be made optional when declared between [].
|
||||||
example26() {
|
example26() {
|
||||||
var _name, _surname, _email;
|
var _name, _surname, _email;
|
||||||
setConfig1({name, surname}) {
|
setConfig1({name, surname}) {
|
||||||
@@ -407,13 +409,13 @@ example26() {
|
|||||||
"email '${_email}'");
|
"email '${_email}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables declared with final can only be set once.
|
/// Variables declared with final can only be set once.
|
||||||
// In case of classes, final instance variables can be set via constant
|
/// In case of classes, final instance variables can be set via constant
|
||||||
// constructor parameter.
|
/// constructor parameter.
|
||||||
class Example27 {
|
class Example27 {
|
||||||
final color1, color2;
|
final color1, color2;
|
||||||
// A little flexibility to set final instance variables with syntax
|
/// A little flexibility to set final instance variables with syntax
|
||||||
// that follows the :
|
/// that follows the :
|
||||||
Example27({this.color1, color2}) : color2 = color2;
|
Example27({this.color1, color2}) : color2 = color2;
|
||||||
}
|
}
|
||||||
example27() {
|
example27() {
|
||||||
@@ -422,11 +424,11 @@ example27() {
|
|||||||
print("Example27 color is '${o.color1}' and '${o.color2}'");
|
print("Example27 color is '${o.color1}' and '${o.color2}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// To import a library, use import "libraryPath" or if it's a core library,
|
/// To import a library, use import "libraryPath" or if it's a core library,
|
||||||
// import "dart:libraryName". There's also the "pub" package management with
|
/// import "dart:libraryName". There's also the "pub" package management with
|
||||||
// its own convention of import "package:packageName".
|
/// its own convention of import "package:packageName".
|
||||||
// See import "dart:collection"; at the top. Imports must come before
|
/// See import "dart:collection"; at the top. Imports must come before
|
||||||
// other code declarations. IterableBase comes from dart:collection.
|
/// other code declarations. IterableBase comes from dart:collection.
|
||||||
class Example28 extends IterableBase {
|
class Example28 extends IterableBase {
|
||||||
var names;
|
var names;
|
||||||
Example28() {
|
Example28() {
|
||||||
@@ -439,11 +441,11 @@ example28() {
|
|||||||
o.forEach((name) => print("Example28 '${name}'"));
|
o.forEach((name) => print("Example28 '${name}'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// For control flow we have:
|
/// For control flow we have:
|
||||||
// * standard switch with must break statements
|
/// * standard switch with must break statements
|
||||||
// * if-else if-else and ternary ..?..:.. operator
|
/// * if-else if-else and ternary ..?..:.. operator
|
||||||
// * closures and anonymous functions
|
/// * closures and anonymous functions
|
||||||
// * break, continue and return statements
|
/// * break, continue and return statements
|
||||||
example29() {
|
example29() {
|
||||||
var v = true ? 30 : 60;
|
var v = true ? 30 : 60;
|
||||||
switch (v) {
|
switch (v) {
|
||||||
@@ -470,17 +472,17 @@ example29() {
|
|||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Never gets here.
|
/// Never gets here.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse int, convert double to int, or just keep int when dividing numbers
|
/// Parse int, convert double to int, or just keep int when dividing numbers
|
||||||
// by using the ~/ operation. Let's play a guess game too.
|
/// by using the ~/ operation. Let's play a guess game too.
|
||||||
example30() {
|
example30() {
|
||||||
var gn, tooHigh = false,
|
var gn, tooHigh = false,
|
||||||
n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
|
n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0;
|
||||||
top = top ~/ 6;
|
top = top ~/ 6;
|
||||||
gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive
|
gn = new DM.Random().nextInt(top + 1); /// +1 because nextInt top is exclusive
|
||||||
print("Example30 Guess a number between 0 and ${top}");
|
print("Example30 Guess a number between 0 and ${top}");
|
||||||
guessNumber(i) {
|
guessNumber(i) {
|
||||||
if (n == gn) {
|
if (n == gn) {
|
||||||
@@ -503,8 +505,8 @@ example30() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional Positional Parameter:
|
/// Optional Positional Parameter:
|
||||||
// parameter will be disclosed with square bracket [ ] & square bracketed parameter are optional.
|
/// parameter will be disclosed with square bracket [ ] & square bracketed parameter are optional.
|
||||||
example31() {
|
example31() {
|
||||||
findVolume31(int length, int breath, [int height]) {
|
findVolume31(int length, int breath, [int height]) {
|
||||||
print('length = $length, breath = $breath, height = $height');
|
print('length = $length, breath = $breath, height = $height');
|
||||||
@@ -514,12 +516,12 @@ example31() {
|
|||||||
findVolume31(10,20); //also valid
|
findVolume31(10,20); //also valid
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional Named Parameter:
|
/// Optional Named Parameter:
|
||||||
// parameter will be disclosed with curly bracket { }
|
/// parameter will be disclosed with curly bracket { }
|
||||||
// curly bracketed parameter are optional.
|
/// curly bracketed parameter are optional.
|
||||||
// have to use parameter name to assign a value which separated with colan :
|
/// have to use parameter name to assign a value which separated with colan :
|
||||||
// in curly bracketed parameter order does not matter
|
/// in curly bracketed parameter order does not matter
|
||||||
// these type parameter help us to avoid confusion while passing value for a function which has many parameter.
|
/// these type parameter help us to avoid confusion while passing value for a function which has many parameter.
|
||||||
example32() {
|
example32() {
|
||||||
findVolume32(int length, int breath, {int height}) {
|
findVolume32(int length, int breath, {int height}) {
|
||||||
print('length = $length, breath = $breath, height = $height');
|
print('length = $length, breath = $breath, height = $height');
|
||||||
@@ -529,9 +531,9 @@ example32() {
|
|||||||
findVolume32(10,20);//also valid
|
findVolume32(10,20);//also valid
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional Default Parameter:
|
/// Optional Default Parameter:
|
||||||
// same like optional named parameter in addition we can assign default value for this parameter.
|
/// same like optional named parameter in addition we can assign default value for this parameter.
|
||||||
// which means no value is passed this default value will be taken.
|
/// which means no value is passed this default value will be taken.
|
||||||
example33() {
|
example33() {
|
||||||
findVolume33(int length, int breath, {int height=10}) {
|
findVolume33(int length, int breath, {int height=10}) {
|
||||||
print('length = $length, breath = $breath, height = $height');
|
print('length = $length, breath = $breath, height = $height');
|
||||||
@@ -541,11 +543,11 @@ example33() {
|
|||||||
findVolume33(10,20);//valid
|
findVolume33(10,20);//valid
|
||||||
}
|
}
|
||||||
|
|
||||||
// Programs have only one entry point in the main function.
|
/// Programs have only one entry point in the main function.
|
||||||
// Nothing is expected to be executed on the outer scope before a program
|
/// Nothing is expected to be executed on the outer scope before a program
|
||||||
// starts running with what's in its main function.
|
/// starts running with what's in its main function.
|
||||||
// This helps with faster loading and even lazily loading of just what
|
/// This helps with faster loading and even lazily loading of just what
|
||||||
// the program needs to startup with.
|
/// the program needs to startup with.
|
||||||
main() {
|
main() {
|
||||||
print("Learn Dart in 15 minutes!");
|
print("Learn Dart in 15 minutes!");
|
||||||
[example1, example2, example3, example4, example5, example6, example7,
|
[example1, example2, example3, example4, example5, example6, example7,
|
||||||
@@ -564,6 +566,3 @@ Dart has a comprehensive web-site. It covers API reference, tutorials, articles
|
|||||||
useful Try Dart online.
|
useful Try Dart online.
|
||||||
[https://www.dartlang.org](https://www.dartlang.org)
|
[https://www.dartlang.org](https://www.dartlang.org)
|
||||||
[https://try.dartlang.org](https://try.dartlang.org)
|
[https://try.dartlang.org](https://try.dartlang.org)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user