text
stringlengths
1
474
some type annotations are optional because
Dart performs type inference.<topic_end>
<topic_start>Creating and assigning variables
In JavaScript, variables cannot be typed.In Dart, variables must either be explicitly
typed or the type system must infer the proper type automatically.
<code_start>/// Dart
/// Both variables are acceptable.
String name = 'dart'; // Explicitly typed as a [String].
var otherName = 'Dart'; // Inferred [String] type.<code_end>
Try it out in DartPad.For more information, see Dart’s Type System.<topic_end>
<topic_start>Default value
In JavaScript, uninitialized variables are undefined.In Dart, uninitialized variables have an initial value of null.
Because numbers are objects in Dart, even uninitialized variables with
numeric types have the value null.info Note
As of 2.12, Dart supports Sound Null Safety,
all underlying types are non-nullable by default,
which must be initialized as a non-nullable value.
<code_start>// Dart
var name; // == null; raises a linter warning
int? x; // == null<code_end>
Try it out in DartPad.For more information, see the documentation on
variables.<topic_end>
<topic_start>
Checking for null or zero
In JavaScript, values of 1 or any non-null objects
are treated as true when using the == comparison operator.In Dart, only the boolean value true is treated as true.
<code_start>/// Dart
var myNull;
var zero = 0;
if (zero == 0) {
print('use "== 0" to check zero');
}<code_end>
Try it out in DartPad.<topic_end>
<topic_start>
Functions
Dart and JavaScript functions are generally similar.
The primary difference is the declaration.
<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);