Flutter Getting Started language -Dart first step (a)

Flutter Getting Started language -Dart first step (a)

Recent study flutter when looked under the official documentation of the sample code, I see a loss, like smoke and mirrors (here @ Northeast sauce). In my personal opinion, Dart language syntax a bit like a combination of java and js, if familiar with the java Pangyou to learn should not be too demanding. So quicken the work, I would like to learn a little based Dart language, and then start my fluter trip.
Reference: https://blog.csdn.net/weixin_33775582/article/details/91377034
http://www.cndartlang.com/669.html

1. tools
I personally use an online compilation tools DartPad , of course there are available for local use IDE, to download it.

2.Helloword!
When learning a new language, we generally dry first thing is the output of a helloword, Dart is no exception. In Dart output helloword very simple.

Here Insert Picture Description

JAVA accustomed to people may be more wondering why there can be a single quote. And look down

3. Data Types
First, we need to be clear, Dart Yes dynamic type of a pure object-oriented language. The Dart everything is an object, including numbers, functions, and so they are inherited from the Object, and the default value is null (including digital) digital Thus, various methods can be called string

Dart in supports the following data types:

A Numbers
Strings
to Booleans
List (that is, arrays)
Maps

First, we look at the string, the string assignment, they can use single quotes, you can also use double quotes (as above helloword). But the point to note is that when using single quotes, double quotes can be embedded in the use of double quotation marks, can be embedded single quotation marks. But it can not use single quotes embedded single quotes, double quotes is the same reason, or need to add an escape character to escape.
As follows:
Here Insert Picture DescriptionSecondly, we are doing string concatenation, you can omit the + sign. Of course you can also use the +. This is because in the previous version of the SDK does not support the use of + connection string.
Here Insert Picture Description
When we need to output a variable value, we need to add a $ symbol. Such as
Here Insert Picture Description
when we need to escape, we can add a sentence to be output before a r, r attention must be next to the quotes, or will be error.
Here Insert Picture Description
4. Keywords: const final
const defined compile-time constants, can only compile-time constants to initialize
final defined constants can be used to initialize a variable
var, final definition of variables such as the time left, and do not care right is not constant
but if the right with the const, then no matter how the left and right must be constant

5. Functions
First, we need to be clear, in the Dart, the function is also an object when our function does not return a value, we will return a null to this method, for example:

void main() {
  
  var name = getName("panghu");
  print(name);
  
}

String getName(String name)
{
  return "name is $name";
}

As the Dart is a dynamically typed language, we can change it to this:

void main() {
  
  var name = getName("panghu");
  print(name);
  
}

 getName( name)
{
  return "name is $name";
}

However, this code will become very large at the time of our difficulty. If the function simply returns the value of an expression, you can use the arrow syntax:

void main() {
  
  var name = getName("panghu");
  print(name);
  
}

 getName( name)=>"name is $name";

Then look at the following code:

var callbacks = [];
for (var i = 0; i < 3; i++) {
  // 在列表 callbacks 中添加一个函数对象,这个函数会记住 for 循环中当前 i 的值。
  callbacks.add(() => print('Save $i')); 
}
callbacks.forEach((c) => c()); // 分别输出 0 1 2

When you call the add method in the for loop, we added to the array it is a function of the object. Then again below the foreach loop, we traverse each time the method is executed again c object (function) itself.
Optional parameter function, Dart language function supports two optional parameters: 1. Name 2. Location optional parameters Optional parameters.

The optional parameters used named braces {}, the default value by a colon:
the position of optional parameters used brackets [], the default value equals sign =

Features:
Name optional parameters: an outer brace is a mandatory parameter, the braces are optional parameters, and in accordance with the key: value of copy form, no defined position.
Position Optional parameters: the outer square brackets is a mandatory parameter, in square brackets are optional, and the order parameter, the assignment of key = value form.

See the examples below

FunX(a, {b, c:3, d:4, e})
{
  print('$a $b $c $d $e');
}

FunY(a, [b, c=3, d=4, e])
{
  print('$a $b $c $d $e');
}


void main()
{
  FunX(1, b:3, d:5);
  FunY(1, 3, 5);
}

Our output is the result:

1 3 3 5 null
1 3 5 4 null

Function closure
concept for js developers should not be unfamiliar, but for java developers is less familiar.
We look at the following example:

Function makeSubstract(num n)
{
  return (num i) => n - i;
}

void main()
{
  var x = makeSubstract(5);
  print(x(2));
}

I try to give you an analysis: in makeSubstract, we return to a Function object type, which is returned to a function, more precisely, we define an anonymous function on the makeSubstract function body. Therefore, in the main function, our x is actually a function object, it actually looks like this:

int x(num i)
{
return 5-i;
}

Function Aliases: See the examples below

typedef int Compare(Object a, Object b);
 
class SortedCollection {
  Compare compare;
 
  SortedCollection(this.compare);
}
 
// Initial, broken implementation.
int sort(Object a, Object b) => 0;
 
void main() {
  SortedCollection coll = new SortedCollection(sort);
  assert(coll.compare is Function);
  assert(coll.compare is Compare);
}

About functions and types on here, next article we look at the other part of the Dart

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104280538