Flutter Getting Started The first step -Dart language (C)

Flutter Getting Started The first step -Dart language (C)

.

Asynchronous support in 1.Dart

Future 1.1:
Future Promise and JavaScript are very similar, showing an asynchronous operation represents the completion of the final (or failure) and result values. In simple terms, it is used to handle asynchronous operations, asynchronous processing is successful on the implementation of a successful operation, asynchronous process has failed to catch errors or stop subsequent operations. A Future will correspond a result, either succeed or fail.

.
1.2 Future.than
we can receive asynchronous than the results, and the corresponding process. In the following example, we simulate a time-consuming operation, and then receives the execution result than in the asynchronous and printed out, as follows:

Future.delayed(new Duration(seconds: 2),(){
   return "hi world!";
}).then((data){
   print(data);
});

.

1.3 Future.catchError
when we perform asynchronous operation errors, we can capture in a catchError, and then make the appropriate treatment. In the above example, we can throw an error, then processing catchError code is as follows:

Future.delayed(new Duration(seconds: 2),(){
//    return "hi world!";
  throw AssertionError("aha!");
}).then((data){
   print(data);
})
  .catchError((e)
{
  print(e);
});

.
1.4 Future.whenComplete
the meaning and finall represented almost similar, after the end of the asynchronous execution on behalf of, regardless of success or failure will ultimately execute code Future.whenComplete in.

Future.delayed(new Duration(seconds: 2),(){
   //return "hi world!";
   throw AssertionError("Error");
}).then((data){
   //执行成功会走到这里 
   print(data);
}).catchError((e){
   //执行失败会走到这里   
   print(e);
}).whenComplete((){
   //无论成功或失败都会走到这里
});

1.4 Future.wait
Sometimes during an asynchronous operation, we need to pay attention to timing issues. For example, two images are all loaded and so ended up re-close the dialog box, which is a parameter wait Furure type of array, and then wait again result in the execution of the code will exist in the array results. code show as below:

Future.wait([
  // 2秒后返回结果  
  Future.delayed(new Duration(seconds: 2), () {
    return "hello";
  }),
  // 4秒后返回结果  
  Future.delayed(new Duration(seconds: 4), () {
    return " world";
  })
]).then((results){
  print(results[0]+results[1]);
}).catchError((e){
  print(e);
});

1.5 Async / await
imagine such a scenario, there are some operations must be based operation has been completed, but because we use the asynchronous operation, if called in order to lose the timing of, only a Future of recall than the next a future, such nested layers more later, lost the code readability and maintainability, dart this phenomenon is called callback hell (callback hell). So Dart, there are other ways to conduct such operations. We look at the following code:

task() async {
   try{
    String id = await login("alice","******");
    String userInfo = await getUserInfo(id);
    await saveUserInfo(userInfo);
    //执行接下来的操作   
   } catch(e){
    //错误处理   
    print(e);   
   }  
}

async used to represent asynchronous function, the function will return a defined Future object, then you can add a method to use a callback function.
await followed by a Future, represents waiting for the asynchronous task is completed, the asynchronous completion will go down; await must appear within the async function.
We can see, we pass async / await an asynchronous stream represented by synchronous code.

In fact, both the JavaScript and Dart in, async / await just a syntactic sugar, compiler or interpreter will eventually be converted into a Promise (Future) call chain.

Stream 1.6
Stream is used for different data receiving asynchronous events, and Future, it can receive a plurality of asynchronous operation result (success or failure). In other words, when performing asynchronous tasks, success or failure can be triggered by events many times to deliver results or data error exception. Stream commonly used in asynchronous tasks will be read many times scene data, such as web content download, read and write files and so on. for example:

Stream.fromFutures([
  // 1秒后返回结果
  Future.delayed(new Duration(seconds: 1), () {
    return "hello 1";
  }),
  // 抛出一个异常
  Future.delayed(new Duration(seconds: 2),(){
    throw AssertionError("Error");
  }),
  // 3秒后返回结果
  Future.delayed(new Duration(seconds: 3), () {
    return "hello 3";
  })
]).listen((data){
   print(data);
}, onError: (e){
   print(e.message);
},onDone: (){

});

Top of the first output code:
hello1, error, hello3

Section on Dart on here, in reserve after this knowledge, we can really begin to learn Flutter, I think there is a little excited. From java to the coder must carefully read the code, because the whole idea of ​​Dart and Java are very different. Next article we started Flutter tour

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

Guess you like

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