1. Obtain test data

  • Interface management site generates test data
    • Random data format website
    • Random avatar access site

2. Integrate network request tools

  • The Dart library
  • pubspec.yamlFile,cupertino_icons:Paste into the library name and version number,Pub getCan be

3. Model transformation

3.1 Map to Json

  • json.encode(value);

3.2 converting from Json to Map

  • json.decode(value);

3.3, create a specific model (pass parameters, return a specific value)

4, asynchronous

  • Multithreading refers to multiple processes A, B, and C, while asynchrony refers to tasks A, B, and C under process A
  • After the method definedasync, added before time-consuming operationawaitWait until the time-consuming operation is complete before performing the next step.
  • Dart specifies that asynchronous functions with async tokens can only be called with await, do not addawaitThe synchronization functions are executed in order

4.1,Future

  • In the future, the data is already wrapped as asynchronous, and it does not affect whether async is added or not when used alone

  • awaitSet:Wait for the Future wrapping task to completeThen execute the following code, andasyncCollocation is used

    • Behind the taskIt has to be asynchronousTo useawaitmodified
    • The current function must also be asynchronous
    • Blocks all subsequent code execution, except if there is other code in the function that should be executed firstawait, and use. Then (value) => Code to be executed after the time-consuming operation is complete
  • .then(value)In thevalueforThe value of the final return in the Future, butIf there is an exception, then. Then () is not executed

  • .then(type value) {code}.catchError((errorMessage){code}).whencomplete ((){code}).timeout(Duration(seconds: 1));

    • Do not initialize direct chained calls

    • Chain call to the instance after initialization

    • Task by task execution

    • Execute after multiple futures are complete

  • CatchError (){onError(){onError(){}

4.2,FutureBuilder

  • Asynchronous rendering, and eventually internal toreturnawidget
  • Uncomplicated interfaces are efficient and do not need to store status and data
2,snapshot.connectionState
  • Asynchronous render state
4.2.2,snapshot.data
  • Asynchronously render data

4.3,scheduleMicrotask(callback)Micro tasks:

  • The Flutter has two queues, one isThe event queue, one isMicrotask queue.Microtask queues have a higher priority than event queues
  • .then() is also a microtask, and the Future will add.then() to the microtask queue, so it has a high priority

Note:

  • sleep()It blocks the main thread

5. Multi-threading

5.1 Characteristics and contrast asynchronous

  • Induction isSingle threadThe Flutter multithreading is moreSimilar to multiple processesBecause theEach thread has its own memory space
  • Because memory is independent, soChanges cannot be invoked directly, and there is no problem with resource preemption, soThere is no lock!

5.2. Modification content

5.2.1,Isolate: isolation
  • Because memory is independent, the inside is isolated from the outside, soChanges require communication
  • After use,Port to be closed,Thread to kill

  • ReceivePort(); Create a port
  • Isolate.spawn(func,port); Create a thread and execute oneA method that takes a port as an argument
  • Port. Listen ((the message)} {code); Listening for port content
  • port.send(message); Sending Port Content
5.2.2,compute(func,param)
  • Variables from child threads can be received directly
  • Internal is the encapsulation of Isolate. Changing external variables in compute(func, PARam) does not take effect

6. State maintenance

  • Mixed withAutomaticKeepAliveClientMixinclass

Personal learning, for reference only, continuous modification