text
stringlengths
1
372
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 threads
finally, 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
StreamBuilder
widgets are used to display the results of a future in the UI.
the following example uses a FutureBuilder:
<code_start>
class HomePage extends StatelessWidget {
const HomePage({super.key});
final HomePageViewModel viewModel = const HomePageViewModel();
@override
widget build(BuildContext context) {
return CupertinoPageScaffold(
// feed a FutureBuilder to your widget tree.
child: FutureBuilder<Weather>(
// specify the future that you want to track.
future: viewModel.load(),
builder: (context, snapshot) {
// a snapshot is of type `asyncsnapshot` and contains the