text
stringlengths
1
372
<topic_start>
flutter for react native developers
this document is for react native (rn) developers looking to apply their
existing RN knowledge to build mobile apps with flutter. if you understand
the fundamentals of the RN framework then you can use this document as a
way to get started learning flutter development.
this document can be used as a cookbook by jumping around and finding
questions that are most relevant to your needs.
<topic_end>
<topic_start>
introduction to dart for JavaScript developers (es6)
like react native, flutter uses reactive-style views. however, while RN
transpiles to native widgets, flutter compiles all the way to native code.
flutter controls each pixel on the screen, which avoids performance problems
caused by the need for a JavaScript bridge.
dart is an easy language to learn and offers the following features:
a few examples of the differences between JavaScript and dart are described
below.
<topic_end>
<topic_start>
entry point
JavaScript doesn’t have a pre-defined entry
function—you define the entry point.
in dart, every app must have a top-level main() function that serves as the
entry point to the app.
<code_start>
/// dart
void main() {}
<code_end>
try it out in DartPad.
<topic_end>
<topic_start>
printing to the console
to print to the console in dart, use print().
<code_start>
/// dart
print('Hello world!');
<code_end>
try it out in DartPad.
<topic_end>
<topic_start>
variables
dart is type safe—it uses a combination of static type checking
and runtime checks to ensure that a variable’s value always matches
the variable’s static type. although types are mandatory,
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.