基本类型
Dart支持以下类型:
Numbers (int, double)
Strings (String)
Booleans (bool)
Records ((value1, value2))
Lists (List, also known as arrays)
Sets (Set)
Maps (Map)
Runes (Runes; often replaced by the characters API)
Symbols (Symbol)
The value null (Null)
Object: The superclass of all Dart classes except Null.
Enum: The superclass of all enums.
Future and Stream: Used in asynchrony support.
Iterable: Used in for-in loops and in synchronous generator functions.
Never: Indicates that an expression can never successfully finish evaluating. Most often used for functions that always throw an exception.
dynamic: Indicates that you want to disable static checking. Usually you should use Object or Object? instead.
void: Indicates that a value is never used. Often used as a return type.
Numbers(int double)
- If num and its subtypes don't have what you're looking for, the dart:math library might.
// int 类型
var x = 1;
var hex = 0xDEADBEEF;
// double 类型
var y = 1.1;
var exponents = 1.42e5;
// num类型(可以是int也可以是double)
num x = 1; // x can have both int and double values
x += 2.5;
// int 可转为 double
double z = 1; // Equivalent to double z = 1.0.
- 将 String 转为 num , num 转为 String
// String -> int
var one = int.parse('1');
assert(one == 1);
// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);
// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');
Strings
// 使用''或""创建String
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
// 两种连接字符串的方式:相邻字面量 或 使用+
var s1 = 'String '
'concatenation'
" works even over line breaks.";
assert(s1 ==
'String concatenation works even over '
'line breaks.');
var s2 = 'The + operator ' + 'works, as well.';
assert(s2 == 'The + operator works, as well.');
// ${expression}嵌入表达式 或 $variable 嵌入变量
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, '
'which is very handy.');
assert('That deserves all caps. '
'${s.toUpperCase()} is very handy!' ==
'That deserves all caps. '
'STRING INTERPOLATION is very handy!');
// 对于多行String 使用'''
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
// r开头创建raw字符串 转义字符等特殊字符会被直接输出,而不会被自动转义;如\n会直接输出,而不会换行
var s = r'In a raw string, not even \n gets special treatment.';
Booleans
由于Dart有类型安全,类似if (nonbooleanValue) or assert (nonbooleanValue)这样的代码不能使用,需按以下方式使用
// Check for an empty string.
var fullName = '';
assert(fullName.isEmpty);
// Check for zero.
var hitPoints = 0;
assert(hitPoints == 0);
// Check for null.
var unicorn = null;
assert(unicorn == null);
// Check for NaN.
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);
Runes and grapheme clusters
import 'package:characters/characters.dart';
void main() {
var hi = 'Hi
标签:assert,gifts,String,List,Dart,学习,002,类型,var
From: https://www.cnblogs.com/ayubene/p/18210067