Does notifyListeners() sometimes complete asynchronously?

Multi tool use
I am calling notifyListeners() towards the end of a class which returns a Future, just before the return statement. The actions initiated by notifyListeners do not fully complete before the return is called and subsequent statements are processed, resulting in errors. It appears that notifyListeners is somehow completing asynchronously.
I asked about this on an alternate forum and was told that this is a common flutter bug, particularly in relation to animation. It seems to me more likely that I am doing something wrong with my syntax. Has anyone come across this 'bug' before?
I tried using both .then() and async / await. Both have the same outcome.
Future<bool> updateProduct(Map<String, dynamic> updatedProduct)
...
return http
.put(
'https://chocolate-f3e81.firebaseio.com/products/${newProduct.serverID}.json',
body: json.encode(newProductMap))
.then<bool>((http.Response response) {
if (response.statusCode == 200 || response.statusCode == 201) {
_products[selectedProductIndex] = newProduct;
setIsLoading(false);
print('model selectedProductIndex ${selectedProductIndex}');
print('model selectedProductServerID ${_selectedProductServerID}');
notifyListeners();
return true;
I expected everything initiated by notifyListeners() to complete before 'return true' is actioned. This does not happen, instead the return is actioned and other code continues and gets ahead of the code initiated by notifyListeners().

New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am calling notifyListeners() towards the end of a class which returns a Future, just before the return statement. The actions initiated by notifyListeners do not fully complete before the return is called and subsequent statements are processed, resulting in errors. It appears that notifyListeners is somehow completing asynchronously.
I asked about this on an alternate forum and was told that this is a common flutter bug, particularly in relation to animation. It seems to me more likely that I am doing something wrong with my syntax. Has anyone come across this 'bug' before?
I tried using both .then() and async / await. Both have the same outcome.
Future<bool> updateProduct(Map<String, dynamic> updatedProduct)
...
return http
.put(
'https://chocolate-f3e81.firebaseio.com/products/${newProduct.serverID}.json',
body: json.encode(newProductMap))
.then<bool>((http.Response response) {
if (response.statusCode == 200 || response.statusCode == 201) {
_products[selectedProductIndex] = newProduct;
setIsLoading(false);
print('model selectedProductIndex ${selectedProductIndex}');
print('model selectedProductServerID ${_selectedProductServerID}');
notifyListeners();
return true;
I expected everything initiated by notifyListeners() to complete before 'return true' is actioned. This does not happen, instead the return is actioned and other code continues and gets ahead of the code initiated by notifyListeners().

New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am calling notifyListeners() towards the end of a class which returns a Future, just before the return statement. The actions initiated by notifyListeners do not fully complete before the return is called and subsequent statements are processed, resulting in errors. It appears that notifyListeners is somehow completing asynchronously.
I asked about this on an alternate forum and was told that this is a common flutter bug, particularly in relation to animation. It seems to me more likely that I am doing something wrong with my syntax. Has anyone come across this 'bug' before?
I tried using both .then() and async / await. Both have the same outcome.
Future<bool> updateProduct(Map<String, dynamic> updatedProduct)
...
return http
.put(
'https://chocolate-f3e81.firebaseio.com/products/${newProduct.serverID}.json',
body: json.encode(newProductMap))
.then<bool>((http.Response response) {
if (response.statusCode == 200 || response.statusCode == 201) {
_products[selectedProductIndex] = newProduct;
setIsLoading(false);
print('model selectedProductIndex ${selectedProductIndex}');
print('model selectedProductServerID ${_selectedProductServerID}');
notifyListeners();
return true;
I expected everything initiated by notifyListeners() to complete before 'return true' is actioned. This does not happen, instead the return is actioned and other code continues and gets ahead of the code initiated by notifyListeners().

New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am calling notifyListeners() towards the end of a class which returns a Future, just before the return statement. The actions initiated by notifyListeners do not fully complete before the return is called and subsequent statements are processed, resulting in errors. It appears that notifyListeners is somehow completing asynchronously.
I asked about this on an alternate forum and was told that this is a common flutter bug, particularly in relation to animation. It seems to me more likely that I am doing something wrong with my syntax. Has anyone come across this 'bug' before?
I tried using both .then() and async / await. Both have the same outcome.
Future<bool> updateProduct(Map<String, dynamic> updatedProduct)
...
return http
.put(
'https://chocolate-f3e81.firebaseio.com/products/${newProduct.serverID}.json',
body: json.encode(newProductMap))
.then<bool>((http.Response response) {
if (response.statusCode == 200 || response.statusCode == 201) {
_products[selectedProductIndex] = newProduct;
setIsLoading(false);
print('model selectedProductIndex ${selectedProductIndex}');
print('model selectedProductServerID ${_selectedProductServerID}');
notifyListeners();
return true;
I expected everything initiated by notifyListeners() to complete before 'return true' is actioned. This does not happen, instead the return is actioned and other code continues and gets ahead of the code initiated by notifyListeners().


New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 27 at 13:15


Dan Horton
61
61
New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Dan Horton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
No. The only way for .then
to be called synchronously is to use SynchronousFuture, which is a custom reimplementation of the future.
If not, then the call is asynchronous even if the future completes immediately.
add a comment |
In fact, the notifyListeners()
will actually behave asynchronously since it schedules a microtask by using scheduleMicrotask()
to call all the registered listeners.
According to the official documentation
Callbacks registered through this function are always executed in
order and are guaranteed to run before other asynchronous events (like
Timer events, or DOM events).
Only comes in mind an ugly solution that might work for your case, which is to run an await Future.delayed(Duration(milliseconds: 300))
just before return true
in order to give it a little time to complete its task.
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Dan Horton is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945722%2fdoes-notifylisteners-sometimes-complete-asynchronously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
No. The only way for .then
to be called synchronously is to use SynchronousFuture, which is a custom reimplementation of the future.
If not, then the call is asynchronous even if the future completes immediately.
add a comment |
No. The only way for .then
to be called synchronously is to use SynchronousFuture, which is a custom reimplementation of the future.
If not, then the call is asynchronous even if the future completes immediately.
add a comment |
No. The only way for .then
to be called synchronously is to use SynchronousFuture, which is a custom reimplementation of the future.
If not, then the call is asynchronous even if the future completes immediately.
No. The only way for .then
to be called synchronously is to use SynchronousFuture, which is a custom reimplementation of the future.
If not, then the call is asynchronous even if the future completes immediately.
answered Dec 27 at 13:31
Rémi Rousselet
24.3k24581
24.3k24581
add a comment |
add a comment |
In fact, the notifyListeners()
will actually behave asynchronously since it schedules a microtask by using scheduleMicrotask()
to call all the registered listeners.
According to the official documentation
Callbacks registered through this function are always executed in
order and are guaranteed to run before other asynchronous events (like
Timer events, or DOM events).
Only comes in mind an ugly solution that might work for your case, which is to run an await Future.delayed(Duration(milliseconds: 300))
just before return true
in order to give it a little time to complete its task.
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
add a comment |
In fact, the notifyListeners()
will actually behave asynchronously since it schedules a microtask by using scheduleMicrotask()
to call all the registered listeners.
According to the official documentation
Callbacks registered through this function are always executed in
order and are guaranteed to run before other asynchronous events (like
Timer events, or DOM events).
Only comes in mind an ugly solution that might work for your case, which is to run an await Future.delayed(Duration(milliseconds: 300))
just before return true
in order to give it a little time to complete its task.
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
add a comment |
In fact, the notifyListeners()
will actually behave asynchronously since it schedules a microtask by using scheduleMicrotask()
to call all the registered listeners.
According to the official documentation
Callbacks registered through this function are always executed in
order and are guaranteed to run before other asynchronous events (like
Timer events, or DOM events).
Only comes in mind an ugly solution that might work for your case, which is to run an await Future.delayed(Duration(milliseconds: 300))
just before return true
in order to give it a little time to complete its task.
In fact, the notifyListeners()
will actually behave asynchronously since it schedules a microtask by using scheduleMicrotask()
to call all the registered listeners.
According to the official documentation
Callbacks registered through this function are always executed in
order and are guaranteed to run before other asynchronous events (like
Timer events, or DOM events).
Only comes in mind an ugly solution that might work for your case, which is to run an await Future.delayed(Duration(milliseconds: 300))
just before return true
in order to give it a little time to complete its task.
answered 2 days ago


miguelpruivo
596212
596212
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
add a comment |
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
1
1
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
Thanks, this is an intelligent and insightful response. From this I conclude that notifyListeners() is not a very reliable way of achieving things, which is rather disappointing. I wonder if there is a benefit to why it works this way, or if there are any plans to 'fix' it. I am left trying to understand when and why I should use notifyListeners(), and what the more reliable alternatives may be. This seems such a fundamental problem, I can't imagine not running up against this all the time. Thank you again, your answer is 100% on target. Any further advice greatly appreciated.
– Dan Horton
2 days ago
add a comment |
Dan Horton is a new contributor. Be nice, and check out our Code of Conduct.
Dan Horton is a new contributor. Be nice, and check out our Code of Conduct.
Dan Horton is a new contributor. Be nice, and check out our Code of Conduct.
Dan Horton is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945722%2fdoes-notifylisteners-sometimes-complete-asynchronously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
UYww8unUeqrMWGM 397mau,0mZuwlLjlmVwcpwxUQuBKvft