text stringlengths 1 372 |
|---|
in that it can push() and pop() routes depending on |
whether you want to navigate to, or back from, a view. |
to navigate between pages, you have a couple options: |
the following example builds a map. |
<code_start> |
void main() { |
runApp( |
MaterialApp( |
home: const MyAppHome(), // becomes the route named '/' |
routes: <string, WidgetBuilder>{ |
'/a': (context) => const MyPage(title: 'page a'), |
'/b': (context) => const MyPage(title: 'page b'), |
'/c': (context) => const MyPage(title: 'page c'), |
}, |
), |
); |
} |
<code_end> |
navigate to a route by pushing its name to the navigator. |
<code_start> |
Navigator.of(context).pushNamed('/b'); |
<code_end> |
the navigator is a stack that manages your app’s routes. |
pushing a route to the stack moves to that route. |
popping a route from the stack, returns to the previous route. |
this is done by awaiting on the future returned by push(). |
async/await is very similar to the .net implementation |
and is explained in more detail in async UI. |
for example, to start a location route |
that lets the user select their location, |
you might do the following: |
<code_start> |
object? coordinates = await Navigator.of(context).pushNamed('/location'); |
<code_end> |
and then, inside your ‘location’ route, once the user has selected their |
location, pop the stack with the result: |
<code_start> |
navigator.of(context).pop({'lat': 43.821757, 'long': -79.226392}); |
<code_end> |
<topic_end> |
<topic_start> |
how do i navigate to another app? |
in Xamarin.Forms, to send the user to another application, |
you use a specific URI scheme, using Device.OpenUrl("mailto://"). |
to implement this functionality in flutter, |
create a native platform integration, or use an existing plugin, |
such asurl_launcher, available with many other packages on pub.dev. |
<topic_end> |
<topic_start> |
async UI |
<topic_end> |
<topic_start> |
what is the equivalent of Device.BeginOnMainThread() in flutter? |
dart has a single-threaded execution model, |
with support for isolates (a way to run dart codes on another thread), |
an event loop, and asynchronous programming. |
unless you spawn an isolate, |
your dart code runs in the main UI thread |
and is driven by an event loop. |
dart’s single-threaded model doesn’t mean you need to run everything |
as a blocking operation that causes the UI to freeze. |
much like Xamarin.Forms, you need to keep the UI thread free. |
you would use async/await to perform tasks, |
where you must wait for the response. |
in flutter, use the asynchronous facilities that the dart language provides, |
also named async/await, to perform asynchronous work. |
this is very similar to c# and should be very easy to use |
for any Xamarin.Forms developer. |
for example, you can run network code without causing the UI to hang by |
using async/await and letting dart do the heavy lifting: |
<code_start> |
future<void> loadData() async { |
final uri dataURL = uri.parse( |
'https://jsonplaceholder.typicode.com/posts', |
); |
final http.Response response = await http.get(dataURL); |
setState(() { |
data = jsonDecode(response.body); |
}); |
} |
<code_end> |
once the awaited network call is done, |
update the UI by calling setState(), |
which triggers a rebuild of the widget subtree and updates the data. |
the following example loads data asynchronously |
and displays it in a ListView: |
<code_start> |
import 'dart:convert'; |
import 'package:flutter/material.dart'; |
import 'package:http/http.dart' as http; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
@override |
widget build(BuildContext context) { |
return const MaterialApp( |
title: 'sample app', |
home: SampleAppPage(), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.