text stringlengths 1 474 |
|---|
responsibilities of a traditional UI object (such as maintaining the |
state of the layout) behind the scenes with RenderObjects. |
RenderObjects persist between frames and Flutter’s lightweight Widgets |
tell the framework to mutate the RenderObjects between states. |
The Flutter framework handles the rest. |
<topic_end> |
<topic_start>Flutter concurrency for Swift developers |
Both Dart and Swift support concurrent programming. |
This guide should help you understand how |
concurrency works in Dart and how it compares to Swift. |
With this understanding, you can create |
high-performing iOS apps.When developing in the Apple ecosystem, |
some tasks might take a long time to complete. |
These tasks include fetching or processing large amounts of data. |
iOS developers typically use Grand Central Dispatch (GCD) |
to schedule tasks using a shared thread pool. |
With GCD, developers add tasks to dispatch queues |
and GCD decides on which thread to execute them.But, GCD spins up threads to |
handle remaining work items. |
This means you can end up with a large number of threads |
and the system can become over committed. |
With Swift, the structured concurrency model reduced the number |
of threads and context switches. |
Now, each core has only one thread.Dart has a single-threaded execution model, |
with support for Isolates, an event loop, and asynchronous code. |
An Isolate is Dart’s implementation of a lightweight thread. |
Unless you spawn an Isolate, your Dart code runs in the |
main UI thread driven by an event loop. |
Flutter’s event loop is |
equivalent to the iOS main loop—in other words, |
the Looper attached to the main thread.Dart’s single-threaded model doesn’t mean |
you are required to run everything |
as a blocking operation that causes the UI to freeze. |
Instead, use the asynchronous |
features that the Dart language provides, |
such as async/await.<topic_end> |
<topic_start> |
Asynchronous Programming |
An asynchronous operation allows other operations |
to execute before it completes. |
Both Dart and Swift support asynchronous functions |
using the async and await keywords. |
In both cases, async marks that a function |
performs asynchronous work, |
and await tells the system to await a result |
from function. This means that the Dart VM could |
suspend the function, if necessary. |
For more details on asynchronous programming, check out |
Concurrency in Dart.<topic_end> |
<topic_start> |
Leveraging the main thread/isolate |
For Apple operating systems, the primary (also called the main) |
thread is where the application begins running. |
Rendering the user interface always happens on the main thread. |
One difference between Swift and Dart is that |
Swift might use different threads for different tasks, |
and Swift doesn’t guarantee which thread is used. |
So, when dispatching UI updates in Swift, |
you might need to ensure that the work occurs on the main thread.Say you want to write a function that fetches the |
weather asynchronously and |
displays the results.In GCD, to manually dispatch a process to the main thread, |
you might do something like the following.First, define the Weather enum:Next, define the view model and mark it as an ObservableObject |
so that it can return a value of type Weather?. |
Use GCD create to a DispatchQueue to |
send the work to the pool of threadsFinally, display the results:More recently, Swift introduced actors to support |
synchronization for shared, mutable state. |
To ensure that work is performed on the main thread, |
define a view model class that is marked as a @MainActor, |
with a load() function that internally calls an |
asynchronous function using Task.Next, define the view model as a state object using @StateObject, |
with a load() function that can be called by the view model:In Dart, all work runs on the main isolate by default. |
To implement the same example in Dart, |
first, create the Weather enum: |
<code_start>enum Weather { |
rainy, |
windy, |
sunny, |
}<code_end> |
Then, define a simple view model (similar to what was created in SwiftUI), |
to fetch the weather. In Dart, a Future object represents a value to be |
provided in the future. A Future is similar to Swift’s ObservableObject. |
In this example, a function within the view model |
returns a Future<Weather> object: |
<code_start>@immutable |
class HomePageViewModel { |
const HomePageViewModel(); |
Future<Weather> load() async { |
await Future.delayed(const Duration(seconds: 1)); |
return Weather.sunny; |
} |
}<code_end> |
The load() function in this example shares |
similarities with the Swift code. |
The Dart function is marked as async because |
it uses the await keyword.Additionally, a Dart function marked as async |
automatically returns a Future. |
In other words, you don’t have to create a |
Future instance manually |
inside functions marked as async.For the last step, display the weather value. |
In Flutter, FutureBuilder and |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.