text
stringlengths 1
474
|
|---|
using a Navigator and Routes.
|
A Route is an abstraction for a Page of an app,
|
and a Navigator is a widget that manages routes.A route roughly maps to a Page.
|
The navigator works in a similar way to the Xamarin.Forms NavigationPage,
|
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(),
|
);
|
}
|
}
|
class SampleAppPage extends StatefulWidget {
|
const SampleAppPage({super.key});
|
@override
|
State<SampleAppPage> createState() => _SampleAppPageState();
|
}
|
class _SampleAppPageState extends State<SampleAppPage> {
|
List<Map<String, dynamic>> data = <Map<String, dynamic>>[];
|
@override
|
void initState() {
|
super.initState();
|
loadData();
|
}
|
Future<void> loadData() async {
|
final Uri dataURL = Uri.parse(
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.