Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why use async function rather than sync ? #5

Closed
Quanhua-Guan opened this issue Nov 13, 2020 · 4 comments
Closed

Why use async function rather than sync ? #5

Quanhua-Guan opened this issue Nov 13, 2020 · 4 comments

Comments

@Quanhua-Guan
Copy link

final FutureOr<void> Function() _execute;

with redo and undo functions being async function, will it be ok to invoke them inside setState((){}) ?

@Quanhua-Guan
Copy link
Author

  /// Notify the framework that the internal state of this object has changed.
  ///
  /// Whenever you change the internal state of a [State] object, make the
  /// change in a function that you pass to [setState]:
  ///
  /// ```dart
  /// setState(() { _myState = newValue; });
  /// ```
  ///
  /// 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.

@Quanhua-Guan
Copy link
Author

Quanhua-Guan commented Nov 13, 2020

setState(() {
_controller.redo();
});

the _controller.redo() runs asynchronously, the state may not changed when the build method finished.

@rodydavis
Copy link
Owner

The api I am built is futureOr meaning it is sync or async but I await all incase it is async

@Quanhua-Guan
Copy link
Author

The api I am built is futureOr meaning it is sync or async but I await all incase it is async

GOT IT! Thanks! (After Dart 2.0, async function will run synchronously before first 'await', so we can make the redo method runs synchronously)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants