Built-in types
supertypes for all types
Object
instead of Any
in Dart the supertype of all types is Object
. In Kotlin it is Any
. For nullable types it is Object?
and Any?
respectively
Enum
is the supertype of all Enums
in Dart there is also a special supertype for all enums
num
is the supertype of all numerical types
in Dart int
and double
has num
as supertype
Numbers
int
(64 bit integer) instead of Int
and Long
in Dart there is no 32bit integer type. Also on the web platform int
is mapped to Javascript number (64-bit floating-point values with no fractional part) and can be from -2^53 to 2^53 - 1.
double
(64 bit floating point) instead of Float
and Double
in Dart there is no 32bit floating point type
Strings
var s1 = `a string`;
var s2= "another string";
var smulti1 = '''
You can create
multi-line strings like this one.
''';
var smulti2 = """This is also a
multi-line string.""";
var sraw = r'In a raw string, not even \n gets special treatment.';
single quotes for strings
Dart allows both single and double quotes for defining string literals. In Kotlin double quotes are required while single quotes define character literals
String
is the same
in Dart String
is a sequence of UTF-16 code units. in Kotlin String
is also internally encoded in utf-16
Booleans
bool
instead of Boolean
see Dart bool documentation. see Kotlin Boolean documentation
Lists
var a_mutable_list = mutableListOf(1,2,3)
a_mutable_list.add(4)
val a_final_list = listOf(1,2,3)
//the proper type annotation for list type
var another_list:List<Int> = listOf(4,5,6)
// kotlin does not have the list spread operator
var joined_list = a_final_list + another_list
println(joined_list); //print [1,2,3,4,5,6]
// kotlin does not have the collection_if syntax
var promoActive= true;
var nav = listOfNotNull("Home", "Furniture", "Plants",
if(promoActive) "Outlet" else null)
println(nav); //print [Home, Furniture, Plants, Outlet]
var a_mutable_list= [1,2,3];
a_mutable_list.add(4);
//cannot add elements to this list and it is a compile time constant
var const_list= const [1,2,3];
//the same as writing const [1,2,3]: cannot add elements
// to this list and it is also a compile time constant
const const_list2= [1,2,3];
//the proper type annotation for list type
List<int> another_list=[4,5,6];
//usage of the spread operator
var joined_list = [0, ...const_list, ...another_list];
print(joined_list); //print [0,1,2,3,4,5,6]
//a null list: in Dart null variables are automatically initialized to null
List<int>? null_list;
//usage of the null-aware spread operator
var joined_list2 = [...const_list, ...?null_list];
print(joined_list2); //print [1,2,3]
//collection_if operator
var promoActive= true;
var nav = ['Home', 'Furniture', 'Plants', if (promoActive) 'Outlet'];
print(nav); //print [Home, Furniture, Plants, Outlet]
//collection_for operator
var listOfInts = [1, 2, 3];
var listOfStrings = ['#0', for (var i in listOfInts) '#$i'];
print(listOfStrings); //print [#0, #1, #2, #3]
supported List
operations are similar (but more limited) to Collection
operations in Kotlin
see Dart List documentation. see Kotlin Collections operations
lists has the collection spread operator ...
, and the null-aware collection spread operator ...?
see Dart List documentation. see also the spread operator proposal. Kotlin does not have the collection spread operator.
lists has the collection_if
construct
see Dart List documentation. see also the control flow collections proposal. Kotlin does not have the collection if syntax.
lists has the collection_for
construct
see Dart List documentation. see also the control flow collections proposal. Kotlin does not have the collection_for syntax.
Sets
// a set
var halogens =
mutableSetOf("fluorine", "chlorine", "bromine", "iodine", "astatine")
// a set with the proper type annotation
var letters:MutableSet<String> = mutableSetOf("a","b","c")
// an empty set
var emptySet= mutableSetOf<String>()
// an immutable set
val constSet = setOf("a","b","c")
//the following line will cause a compilation error
//constSet.add('d');
// a set
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
// a set with the proper type annotation
Set<String> letters = {'a','b','c'};
// an empty set
var emptySet= <String>{};
//this create a map not a set!
var emptyMap = {};
// a constant set
const constSet = {'a','b','c'};
//the following operation will throw an exception
//constSet.add('d');
see the supported Set
operations
see also the Kotlin supported Set
operations
sets has the collection spread operator ...
, and the null-aware collection spread operator ...?
see Dart spread operator documentation. see also the spread operator proposal. Kotlin does not have the collection spread operator.
sets has the collection_if
construct
see Dart collection_if documentation. see also the control flow collections proposal.
Kotlin does not have the collection if syntax.
sets has the collection_for
construct
see Dart collection_for documentation. see also the control flow collections proposal. Kotlin does not have the collection_for syntax.
Maps
//a Map<Int,String> object
var nobleGases = mutableMapOf(
2 to "helium",
10 to "neon",
18 to "argon",
)
//a map with explicit type annotation
var nobleGases2:MutableMap<Int, String> = mutableMapOf()
//an immutable map
val constantMap = mapOf(
2 to "helium",
10 to "neon",
18 to "argon",
)
//constantMap[2] = 'Helium'; // This line will not compile
//a Map<Int,String> object
var nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon',
};
//a map with explicit type annotation
Map<int, String> nobleGases2 = {};
//a constant map
final constantMap = const {
2: 'helium',
10: 'neon',
18: 'argon',
};
constantMap[2] = 'Helium'; // This line will throw an exception.
see the supported Map
operations
see also the Kotlin supported Map
operations
maps has the collection spread operator ...
, and the null-aware collection spread operator ...?
see Dart spread operator proposal. Kotlin does not have the collection spread operator.
sets has the collection_if
construct
see Dart control flow collections proposal. Kotlin does not have the collection if syntax.
sets has the collection_for
construct
see Dart control flow collections proposal. Kotlin does not have the collection_for syntax.