- 내가 보려고 작성
- 원본링크 dart.dev
잘 이해가 안되는것
main() {
var name = 'test';
Object name1 = 'bob';
String name2 = '';
}
int no; //error (함수 안에서는 가능 단 사용하기 전에 초기화 해야함)
int no = 1; //no error
int? lineCount; //nullable
late String name3;
main() {
const name = 'test';
final name = 'test';
var name = const[]; //unmodifiable list
const name = []; //이것도 위에랑 동일함
}
main() {
var x = 1;
var hex = 0xDEADBEEF;
var exponent = 8e5;
num x = 1; // x can have both int and double values
x += 2.5;
double z = 1; // Equivalent to double z = 1.0.
}
// String -> int
var one = int.parse('1');
// String -> double
var onePointOne = double.parse('1.1');
// int -> String
String oneAsString = 1.toString();
// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
main() {
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
}
main() {
var s1 = 'String '
'concatenation'
" works even over line breaks.";
var s2 = 'The + operator ' + 'works, as well.';
}
main() {
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
}
main() {
var list = [
'Car',
'Boat',
'Plane',
];
}
main() {
var list = [1, 2, 3];
var list2 = [...list, 4];
}
main() {
bool promoActive = true;
var nav = ['Home', 'Furniture', 'Plants', if (promoActive) 'Outlet'];
var listOfInts = [1, 2, 3];
var listOfStrings = [
'#0',
for (var i in listOfInts) '#$i'
];
}
main() {
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
}
main() {
var names = <String>{};
Set<String> names = {};
}
main() {
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
}
import 'package:characters/characters.dart';
void main() {
var hi = 'Hi 🇩🇰';
print(hi);
print('The end of the string: ${hi.substring(hi.length - 1)}');
print('The last character: ${hi.characters.last}\n');
}
bool isNoble(int atomicNumber) {
return _nobleGases[atomicNumber] != null;
}
//스크립트 언어처럼 선언가능
bool isNoble(int atomicNumber) => _nobleGases[atomicNumber] != null;
void enableFlags({bool? bold, bool? hidden}) {
}
main(List<String> arguments) {
String say(String from, String msg, [String? device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
//위 함수는 아래처럼 초기화 시키면서 넘길수도 있음
String say1(String from, String msg, [String device = 'carrier pigeon']) {
var result = '$from says $msg with a $device';
return result;
}
print(say('test', 'zxcv')); //이렇게 가능하고
print(say('test', 'zxcv', 'iphone')); //이렇게도 가능함
}
main() {
void printElement(int element) {
print(element);
}
var list = [1, 2, 3];
// Pass printElement as a parameter.
list.forEach(printElement);
}
main() {
const list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
print('${list.indexOf(item)}: $item');
});
}
main() {
bool topLevel = true;
void main() {
var insideMain = true;
void myFunction() {
var insideFunction = true;
void nestedFunction() {
var insideNestedFunction = true;
assert(topLevel);
assert(insideMain);
assert(insideFunction);
assert(insideNestedFunction);
}
}
}
}
Function makeAdder(int addBy) {
return (int i) => addBy + i;
}
void main(List<String> arguments) {
var add2 = makeAdder(2);
print(add2);
print(add2(1));
var add4 = makeAdder(4);
print(add4);
print(add4(3));
}
Result Closure: (int) => int 3 Closure: (int) => int 7
//일반적인 방식
var visibility = isPublic ? 'public' : 'private';
//??로 표현한 방법
String playerName(String? name) => name ?? 'Guest';
void main(List<String> arguments) {
String playerName(String? name) => name ?? 'Guest';
print('null : ${playerName(null)}');
print('value : ${playerName('test')}');
}
class Man {
late String name;
late int age;
printMan() {
print("man : $name $age");
}
}
void main() {
var man = Man()
..name = 'test'
..age = 16
..printMan();
}
Result man : test 16
main() {
if (isRaining()) {
you.bringRainCoat();
} else if (isSnowing()) {
you.wearJacket();
} else {
car.putTopDown();
}
}
void main() {
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
for (var callback in callbacks) {
callback();
}
}
main() {
try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
}
class Point {
double? x; // Declare instance variable x, initially null.
double? y; // Declare y, initially null.
double z = 0; // Declare z, initially 0.
late double x1;
}
< + | >>> > / ^ [] <= ~/ & []= >= * << ~ – % >> ==
class Vector {
final num x, y;
Vector(this.x, this.y);
Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
Vector operator -(Vector v) => Vector(x - v.x, y - v.y);
Vector operator /(Vector v) => Vector(x / v.x, x / v.y);
}
void main() {
final v = Vector(2, 2);
final w = Vector(4, 10);
final t = v + w;
final n = v / w;
print('${t.x},${t.y}');
print('${n.x},${n.y}');
}
Result 6,12 0.5,0.2
class A {}
class B {}
class D {}
class E {
print1() {
print('e print');
}
}
mixin F {
print1() {
print('f print');
}
}
class C extends A with B, E, D, F {}
//class C extends A with B, D, F, E {}
void main() {
var c = C();
c.print1();
}
class Man {}
mixin Man1 on Man {}
class C with Man1 {} //사용불가
class Man2 extends Man with Man1 {} //사용 가능