setState() doesn't execute until the enclosing function returns

前端 未结 2 377
终归单人心
终归单人心 2021-01-24 21:16

So I was trying this code in flutter:

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyA         


        
2条回答
  •  伪装坚强ぢ
    2021-01-24 22:14

    Event Loop

    • There is something called Event Loop

    • Event Loop process Events in order

    You have two events in order

    Event A => Click => by the User

        0.onPressed: () {
            1.setState(() {
                3. i++
                4. Mark as widget dirty
                5. Add to the global dirty widgets list
            });
            6.i++
        });
    

    Event B => Vsync signal => provided by the OS

        7. check dirty widgets list
        8. repaint
    

    More

    • Here a YouTube video from Flutter in Focus series

      • Isolates and Event Loops

        • by flutter team member Andrew Brogdon (@RedBrogdon)
    • Or read it here

      • Dart asynchronous programming: Isolates and event loops

    Ref.:

    1. setState method - State class - widgets library - Dart API

    2. markNeedsBuild method - Element class - widgets library - Dart API

    3. scheduleBuildFor method - BuildOwner class - widgets library - Dart API

    4. drawFrame method - WidgetsBinding class - widgets library - Dart API

    5. handleDrawFrame method - SchedulerBinding class - scheduler library - Dart API

    6. buildScope method - BuildOwner class - widgets library - Dart API

    7. dart engine loop - Google Search

    8. Dart Programming - Loops - Tutorialspoint

    9. optimization - What is the optimal render loop in Dart 2? - Stack Overflow

    10. Understanding Flutter Render Engine - Stack Overflow

    11. Technical overview - Flutter

    12. Flutter - Dart API docs

    13. flutter/spinning_square.dart at master · flutter/flutter

    14 .dart engine - Google Search

    1. scheduler library - Dart API

    2. flutter/binding.dart at master · flutter/flutter

    3. scheduler library - Dart API

    4. frame scheduling flutter - Google Search

    5. scheduleFrame method - SchedulerBinding class - scheduler library - Dart API

    6. scheduler library - Dart API

    7. packages/flutter/lib/scheduler.dart - external/github.com/flutter/flutter - Git at Google

    8. flutter/spinning_square.dart at master · flutter/flutter

    9. dart engine - Google Search

    10. threading | Dart Package

    11. isolate flutter - Google Search

提交回复
热议问题