text stringlengths 1 372 |
|---|
_errorText = null; |
} |
}); |
}, |
decoration: InputDecoration( |
hintText: 'this is a hint', |
errorText: _errorText, |
), |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
threading & asynchronicity |
this section discusses concurrency in flutter and |
how it compares with UIKit. |
<topic_end> |
<topic_start> |
writing asynchronous code |
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 the iOS main loop—that is, |
the looper that is attached to the main thread. |
dart’s single-threaded model doesn’t mean you are |
required to run everything as a blocking operation |
that causes the UI to freeze. instead, |
use the asynchronous facilities that the dart language provides, |
such as async/await, to perform asynchronous work. |
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('https://jsonplaceholder.typicode.com/posts'); |
final http.Response response = await http.get(dataURL); |
setState(() { |
data = jsonDecode(response.body); |
}); |
} |
widget getRow(int index) { |
return padding( |
padding: const EdgeInsets.all(10), |
child: Text('Row ${data[index]['title']}'), |
); |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.