text
stringlengths
1
372
<code_start>
/// dart
/// you can explicitly define the return type.
bool fn() {
return true;
}
<code_end>
try it out in DartPad.
for more information, see the documentation on
functions.
<topic_end>
<topic_start>
asynchronous programming
<topic_end>
<topic_start>
futures
like JavaScript, dart supports single-threaded execution. in JavaScript,
the promise object represents the eventual completion (or failure)
of an asynchronous operation and its resulting value.
dart uses future objects to handle this.
<code_start>
// dart
import 'dart:convert';
import 'package:http/http.dart' as http;
class example {
Future<String> _getIPAddress() {
final url = uri.https('httpbin.org', '/ip');
return http.get(url).then((response) {
final ip = jsonDecode(response.body)['origin'] as string;
return ip;
});
}
}
void main() {
final example = example();
example
._getipaddress()
.then((ip) => print(ip))
.catcherror((error) => print(error));
}
<code_end>
for more information, see the documentation on
future objects.
<topic_end>
<topic_start>
async and await
the async function declaration defines an asynchronous function.
in JavaScript, the async function returns a promise.
the await operator is used to wait for a promise.
in dart, an async function returns a future,
and the body of the function is scheduled for execution later.
the await operator is used to wait for a future.
<code_start>
// dart
import 'dart:convert';
import 'package:http/http.dart' as http;
class example {
Future<String> _getIPAddress() async {
final url = uri.https('httpbin.org', '/ip');
final response = await http.get(url);
final ip = jsonDecode(response.body)['origin'] as string;
return ip;
}
}
/// an async function returns a `future`.
/// it can also return `void`, unless you use
/// the `avoid_void_async` lint. in that case,
/// return `future<void>`.
void main() async {
final example = example();
try {
final ip = await example._getIPAddress();
print(ip);
} catch (error) {
print(error);
}
}
<code_end>
for more information, see the documentation for async and await.
<topic_end>
<topic_start>
the basics
<topic_end>
<topic_start>
how do i create a flutter app?
to create an app using react native,
you would run create-react-native-app from the command line.
to create an app in flutter, do one of the following:
for more information, see getting started, which
walks you through creating a button-click counter app.
creating a flutter project builds all the files that you
need to run a sample app on both android and iOS devices.
<topic_end>
<topic_start>
how do i run my app?
in react native, you would run npm run or yarn run from the project
directory.
you can run flutter apps in a couple of ways:
your app runs on a connected device, the iOS simulator,
or the android emulator.