text
stringlengths 1
80
|
---|
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鈥檚 event |
loop is equivalent to android鈥檚 main looper鈥攖hat is, the looper that |
is attached to the main thread. |
dart鈥檚 single-threaded model doesn鈥檛 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鈥檝e used it in c#, javascript, or if you |
have used kotlin鈥檚 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'); |
http.Response response = await http.get(dataURL); |
setState(() { |
widgets = 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 |