text
stringlengths 1
474
|
---|
'/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 other popular use-case for Intents is to call external components such |
as a Camera or File picker. For this, you would need to create a native platform |
integration (or use an existing plugin).To learn how to build a native platform integration, |
see developing packages and plugins.<topic_end> |
<topic_start> |
How do I handle incoming intents from external applications in Flutter? |
Flutter can handle incoming intents from Android by directly talking to the |
Android layer and requesting the data that was shared.The following example registers a text share intent filter on the native |
activity that runs our Flutter code, so other apps can share text with |
our Flutter app.The basic flow implies that we first handle the shared text data on the |
Android native side (in our Activity), and then wait until Flutter requests |
for the data to provide it using a MethodChannel.First, register the intent filter for all intents in AndroidManifest.xml:Then in MainActivity, handle the intent, extract the text that was |
shared from the intent, and hold onto it. When Flutter is ready to process, |
it requests the data using a platform channel, and it’s sent |
across from the native side:Finally, request the data from the Flutter side |
when the widget is rendered: |
<code_start>import 'package:flutter/material.dart'; |
import 'package:flutter/services.dart'; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
// This widget is the root of your application. |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Sample Shared App Handler', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
static const platform = MethodChannel('app.channel.shared.data'); |
String dataShared = 'No data'; |
@override |
void initState() { |
super.initState(); |
getSharedText(); |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold(body: Center(child: Text(dataShared))); |
} |
Future<void> getSharedText() async { |
var sharedData = await platform.invokeMethod('getSharedText'); |
if (sharedData != null) { |
setState(() { |
dataShared = sharedData; |
}); |
} |
} |
}<code_end> |
<topic_end> |
<topic_start> |
What is the equivalent of startActivityForResult()? |
The Navigator class handles routing in Flutter and is used to get |
a result back from a route that you have pushed on the stack. |
This is done by awaiting on the Future returned by push().For example, to start a location route that lets the user select |
their location, you could 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 |
you can pop the stack with the result: |
<code_start>Navigator.of(context).pop({'lat': 43.821757, 'long': -79.226392});<code_end> |
<topic_end> |
<topic_start> |
Async UI |
<topic_end> |
<topic_start> |
What is the equivalent of runOnUiThread() in Flutter? |
Dart has a single-threaded execution model, with support for Isolates |
(a way to run Dart code 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. Flutter’s event |
loop is equivalent to Android’s main Looper—that is, the Looper that |
is attached to the main thread.Dart’s single-threaded model doesn’t mean you need to run everything as a |
blocking operation that causes the UI to freeze. Unlike Android, which |
requires you to keep the main thread free at all times, in Flutter, |
use the asynchronous facilities that the Dart language provides, such as |
async/await, to perform asynchronous work. You might be familiar with |
the async/await paradigm if you’ve used it in C#, Javascript, or if you |
have used Kotlin’s coroutines.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 { |
var dataURL = Uri.parse('https://jsonplaceholder.typicode.com/posts'); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.