Flutter 강의 (1)

이성은·2023년 7월 15일
post-thumbnail

초기 환경설정

  • flutter doctor
    [✓] Flutter (Channel stable, 3.10.6, on macOS 13.2.1 22D68 darwin-arm64, locale ko-KR)
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    [✓] Chrome - develop for the web
    [✓] Android Studio (version 2022.2)
    [✓] VS Code (version 1.80.0)
    [✓] Connected device (2 available)
    [✓] Network resources

Dart

void main() {
  print('Hello, World!');
}
  • 주석
    • // 일반적인 주석
    • ///
  • 변수 선언
var name = 'Voyager I';
var year = 1977;
var antennaDiameter = 3.7;
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
var image = {
  'tags': ['saturn'],
  'url': '//path/to/saturn.jpg'
};
  • 조건문과 반복문
if (year >= 2001) {
  print('21st century');
} else if (year >= 1901) {
  print('20th century');
}

for (final object in flybyObjects) {
  print(object);
}

for (int month = 1; month <= 12; month++) {
  print(month);
}

while (year < 2016) {
  year += 1;
}
  • 함수
int fibonacci(int n) {
  if (n == 0 || n == 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

var result = fibonacci(20);
  • 화살표 함수
flybyObjects.where((name) => name.contains('turn')).forEach(print);
  • Imports : 다른 라이브러리들의 APIs를 가져다가 씀
  • class : 설계도라고 생각, 재활용해서 사용
  • enum : 여러 개의 타입 설정, 열거형, 확장할수 있음
enum PlanetType { terrestrial, gas, ice }
  • 상속: class를 확장 (extends)
class Orbiter extends Spacecraft {
  double altitude;

  Orbiter(super.name, DateTime super.launchDate, this.altitude);
}
  • mixin : 다중 class 상속, with 사용
mixin Piloted {
  int astronauts = 1;

  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

class PilotedCraft extends Spacecraft with Piloted {
  // ···
}
  • Interfaces
  • abstract
  • Async : callback hell를 피함, 비동기, async와 await사용
    • future : 비동기 작업의 결과를 나타내며 미완료(value를 생성하기 전)또는 완료(value 생성)의 두 가지 상태를 가질 수 있다.
  • Exceptions : 예외상황을 정의
if (astronauts == 0) {
  throw StateError('No astronauts.');
}

  try {
    for (final object in flybyObjects) {
      var description = await File('$object.txt').readAsString();
      print(description);
    }
  } on IOException catch (e) {
    print('Could not describe object: $e');
  } finally // finally : 예외 발생해도 무조건 처리해라.
  {
    flybyObjects.clear();
  }
}

변수

  • var , 타입을 알수 없기 때문에, 권장하지 않음
  • Object
  • 원시타입
    • String : 문자열
    • Int : 정수
    • Double : 소수점 숫자( 1.1 ..)
  • Null safety
    • null : 아무 값이 할당 안됨
    String name; //null이 될수 없다. 무조건 값을 가져야한다.
    String? name; //null이 될수 있다.
  • Default value : 초기값
 int lineCount;

if (weLikeToCount) {
  lineCount = countLines(); 
  //weLikeToCount를 만족하면 countLines()실행하고 그것을 초기값으로으로 설정하겠다.
} else {
  lineCount = 0; //아니면 0으로 초기값을 가지겠다.
}

print(lineCount);
  • late
     late String name; //null이 될수 없다. late: 나중에 무조건 값을 가져야한다.
  • Final and const : 상수값, 바꿀수 없다. 컴파일러가 메모리상에 최적화하고, 프로그램 성능에 도움이 될수 있다.
    • 컴파일 타임: 우리가 쓴 코드를 컴터가 알아들을 수 있는 언어로 바꿈
    • 런타임: 코드가 실행되는 타임
    • const: 컴파일러가 컴파일과정에서 최적화를 수행하겠다.
    • final: 런타임시에 최적화를 수행하겠다.
    • https://velog.io/@ruinak_4127/Dart-final%EA%B3%BC-const%EC%9D%98-%EC%B0%A8%EC%9D%B4 참고
      final name = 'Bob'; // Without a type annotation 
      final String nickname = 'Bobby';
  • List
const Object i = 3; // Where i is a const Object with an int value...
const list = [i as int]; // Use a typecast.
const map = {if (i is int) i: 'int'}; // Use is and collection if.
const set = {if (list is List<int>) ...list}; // ...and a spread.
  • Bool : true, false

Operators(연산)

  • https://dart.dev/language/operators
  • ~/ : 결과를 나눠서 정수로 나타내주세요, 즉 double타입은 안받음
  • shift : << >> >>> , 속도가 빠름, 잘 사용은 안한다.
  • XOR : ^
  • OR : |
  • AND : &
  • as is is!
  • ==,!=
  • && : a조건과 b조건이 같다 다르다
  • if null:?? , 값이 null이라면 이값을 넣어줘
profile
함께 일하는 프론트엔드 개발자 이성은입니다🐥

0개의 댓글