gpt4 book ai didi

Flutter:setState() 在封闭函数返回之前不会执行

转载 作者:行者123 更新时间:2023-12-03 04:15:15 24 4
gpt4 key购买 nike

所以我在 flutter 中尝试这段代码:

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

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

class _MyAppState extends State<MyApp> {
int _i = 1;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MaterialButton(
child: Text('You Pressed Me $_i'),
onPressed: () {
setState(() {
_i++;
print('inside i = $_i');
});
sleep(Duration(seconds: 10));
_i++;
print('outside i = $_i');
}
),
),
),
);
}
}

预期行为(运行并按下按钮一次后):按钮显示文字“You Pressed Me 2”

然后变量 _i 会增加到 3 而不会影响视觉效果。

实际行为: setState() 被执行,然后 _i 再次递增,并且没有发生视觉变化,即屏幕上的文本没有t 更新,当 onPressed() 返回时,setState() 会导致小部件重建并更新屏幕,这是 10 秒后出现在屏幕上的内容: “你压了我 3”.

这句话来自 docs关于setState():

The provided callback is immediately called synchronously. It must not return a future (the callback cannot be async), since then it would be unclear when the state was actually being set.

我知道调用是同步的(因此它是阻塞的,根据 this 的回答)所以它应该首先返回(并且这已经发生了)然后更新屏幕(或安排在将来的某个时间)和然后将控制权返回到下一行(后两件事不会发生)。

我什至在没有 sleep 的情况下尝试过,但结果相同。

那么我错过了什么或误解了什么?

最佳答案

事件循环

  • 有一种叫做事件循环的东西

  • 事件循环按顺序处理事件

您有两个事件

事件 A => 用户点击 =>

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

事件 B => Vsync 信号 => 操作系统提供

    7. check dirty widgets list
8. repaint

更多

引用:

  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

enter image description here

关于Flutter:setState() 在封闭函数返回之前不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com