Developing with Flutter for 4 and half years and just started using Completer a few days ago, now this video pops out. I am working on package for zebra printers with native SDK, using jnigen package and this technique helped me. On native (Kotlin) side I need to make coroutines to be able to print, so I needed a way to handle this on Dart side. I pass a callback class containing operation results (success/error), which I use them to call completer.complete/completer.completeError when the methods finish on Kotlin side. Very handy!
This is a really useful technique to me. It was helpful for the case when there is some ongoing background task which can be paused or interrupted by user actions. You can also use them instead of bool flags to track if some async task is ongoing/completed. And you can manually pass value with which they resolve.
This is is a very useful signaling mechanism, it is in many languages with different names(C# - TaskCompletionSource ) and ive made on in JS using promises :D.
I think the only way to force cancelling a process would be something similar to creating an isolate and destroying it when you dont want it anymore. But I'm not sure if that would work (just a guess) and I am sure that would be a big boilerplate for something that we can all just safely ignore today. If the call is already resource intensive you would probably be using isolates anyway
How would that work? Cancellation in Dart must be cooperative. It’s a hard problem, it’s impossible in Dart to “just” cancel an invoked asynchronous function.
@@TekExplorer I’m struggling to understand this concept. If I used a try catch and I set my bool variable simply after the await call, why is that not enough? And how do completers solve the problem
@@JoeRomano-s8g The inner property completer.future is a future value of the type you set for the completer. It will always need an await so if it's not complete, it will not leave the "awaiting" part of your code. If it was already completed previously, it resolves immediately as said in the vídeo.
@flutterdev What tool did you guys use for the presentation / infographic? I am looking to make some myself in the same style you guys use. It is for my fellow students at my university. Really think it works well.
It seems like redundant work. One don't need Completer in this context. One can do simple like that: ```dart Future init () async { return await expensiveSetup(); } ``` and then ```dart final obj = MyClass(); await obj.init(); obj.doStuff(); ```
What if you have multiple places that need to await the computation? So if I have a service that needs to be initialised. Then I have 4 other controllers needing this one service. The 4 can all await the completer. But it can't await the init. Why? Because if you run init by 4 controllers, the init process will run 4 times.
Thanks Dev team! Now my app doesn't need a button to increment the counter! I wish there was more competent documentation, which would improve the simplistic and pitiful explanation. I asked Gemini if he could explain the technique better, applying it to real cases with isolate, etc. s/He told me that my question offended h_er/im and closed the chat.
Hey guys, I have to implement video calling using flutter for 3 platforms (web, android, ios ) any suggestions which package provides solutions for all 3 platforms.
"When working with external APIs or libraries that do not return Futures directly, Completers can be used to wrap and adapt these operations into Futures." - So this can be done with Futures but is used when calling code that doesn't return a Future?
Watch more Technique of the Week → goo.gle/FlutterTechnique
Developing with Flutter for 4 and half years and just started using Completer a few days ago, now this video pops out.
I am working on package for zebra printers with native SDK, using jnigen package and this technique helped me.
On native (Kotlin) side I need to make coroutines to be able to print, so I needed a way to handle this on Dart side. I pass a callback class containing operation results (success/error), which I use them to call completer.complete/completer.completeError when the methods finish on Kotlin side. Very handy!
It's like use states with bloc or not?
This is such an underused technique! I use it everywhere
Don’t
Why? Just curious please. I don't know better@@jaleger2295
@@jaleger2295 why??
Unneccessary thing
in C# it's also a super common pattern to use it. It's not called like that but it's basically the same pattern
I've been using Completers non-stop everywhere in my app nowadays. Such a convenient technique
Remember the KISS principle.
No need to adjust to this concept if your system does not require you to.
Thanks for the dart pad sample. Its a good move going forward to keep such tricks with a dart pad sample.
This is a really useful technique to me. It was helpful for the case when there is some ongoing background task which can be paused or interrupted by user actions. You can also use them instead of bool flags to track if some async task is ongoing/completed. And you can manually pass value with which they resolve.
Nice Technique, i would use in the next project.!!
great platform to learn flutter
@flutterdev Completer feels like generator function of JavaScript. Am I right ?
Streams feels like RxJS streams. Nice update.
This is is a very useful signaling mechanism, it is in many languages with different names(C# - TaskCompletionSource ) and ive made on in JS using promises :D.
Too bad that when canceling a completer it doesn’t really cancel the task just ignores the result
I think the only way to force cancelling a process would be something similar to creating an isolate and destroying it when you dont want it anymore. But I'm not sure if that would work (just a guess) and I am sure that would be a big boilerplate for something that we can all just safely ignore today.
If the call is already resource intensive you would probably be using isolates anyway
That's true. Only way to cancel task is wrap it in isolate.
How would that work? Cancellation in Dart must be cooperative. It’s a hard problem, it’s impossible in Dart to “just” cancel an invoked asynchronous function.
Looks like a promise with the resolve callback isn't it 😄
Craig, I'd love to see a colab with guys at Rive and you, they have their Gamekit written in Flutter...
огонь, лучшая подработка в моей жизни
Good 👍
Why would i use this when i can assign simply a bool value if future result is done successfully or not
because you cant await that
@@TekExplorer
I’m struggling to understand this concept. If I used a try catch and I set my bool variable simply after the await call, why is that not enough? And how do completers solve the problem
@@TekExplorer explain how can you not await it???
I think the same 😅
@@JoeRomano-s8g
The inner property completer.future is a future value of the type you set for the completer. It will always need an await so if it's not complete, it will not leave the "awaiting" part of your code. If it was already completed previously, it resolves immediately as said in the vídeo.
good video! but i think it's better if you go live on twitch every day or often and we imitate all of your trades
@flutterdev What tool did you guys use for the presentation / infographic? I am looking to make some myself in the same style you guys use. It is for my fellow students at my university. Really think it works well.
It seems like redundant work. One don't need Completer in this context. One can do simple like that:
```dart
Future init () async {
return await expensiveSetup();
}
```
and then
```dart
final obj = MyClass();
await obj.init();
obj.doStuff();
```
I agree, I don't see how completers add anything new.
overEngineering at his best
What if you have multiple places that need to await the computation? So if I have a service that needs to be initialised. Then I have 4 other controllers needing this one service. The 4 can all await the completer. But it can't await the init. Why? Because if you run init by 4 controllers, the init process will run 4 times.
Thanks Dev team! Now my app doesn't need a button to increment the counter! I wish there was more competent documentation, which would improve the simplistic and pitiful explanation.
I asked Gemini if he could explain the technique better, applying it to real cases with isolate, etc.
s/He told me that my question offended h_er/im and closed the chat.
this is so smart omg thanks !!
That's exactly what I desired :)
Реал рабочая связка, спасибо бро
on second usecase is completer is used for ensure input value’s operation is asynchronously finished until output stream’s emit??
Hey guys, I have to implement video calling using flutter for 3 platforms (web, android, ios ) any suggestions which package provides solutions for all 3 platforms.
But why don't we just use a nullable variable and then initialize method future to start using the variable?
I will just use a bool my class and an infinite loop over the implementation
So this is similar to Promise
❤❤❤
Some kind of semaphore on steroids ))))
Is this just covering a limitation of Futures? Feel like in JS this is just solved with a new Promise, is there something I'm missing?
"When working with external APIs or libraries that do not return Futures directly, Completers can be used to wrap and adapt these operations into Futures." - So this can be done with Futures but is used when calling code that doesn't return a Future?
interessante
😊
🎉
👍
Who said flutter id died? Long life flutter
Your videos are too bright, please use darker colors