* 클래스 직렬화 (Json)
- Flutter는 간단한 json 인코더와 디코더가 내장된 dart:convert 라이브러리 가짐
- fromJson : map 구조에서 새로운 User 객체 생성하기 위한 생성자
- toJson : User 객체를 map 구조로 변환하기 위한 메서드
//user.dart
class User {
final String name;
final String email;
User(this.name, this.email);
User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
Map<String, dynamic> toJson() => {
'name': name,
'email': email,
};
}
//main.dart
import 'user.dart';
void main() {
User userToJson = User("cy", "cy@abc.com");
print(userToJson.toJson());
User jsonToUser = User.fromJson(userToJson.toJson());
print(jsonToUser.name);
print(jsonToUser.email);
}
//결과
{name: cy, email: cy@abc.com}
cy
cy@abc.com
같이 보면 좋은 포스팅
https://cording-cossk3.tistory.com/88
[Flutter] Dart 문법 (skip, take)
* skip, take - skip : 첫 번째 (count) 요소 제외한 모든 요소 제공하는 iterable 반환, 즉 count개 요소 제외한 모든 요소 - take : 이 iterable의 첫 번째 요소 (count) 개의 lazy iterable 반환, 즉 iterable의 첫 번째 요
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/89
[Flutter] Dart 문법(??=, ??, null 인식 연산자, cascade, ..) #flutter operater ?? #flutter null #dart ..
* null 인식 연산자 : ??=, ?? * cascade (..연산자) : 자바의 builder 패턴과 유사. 객체 레퍼런스 변수 없이 바로 값 대입 가능 class Animal { var name = "dog"; var age; var sound; } main() { var a = 3; // ??= : 변수가 현재
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/90
[Flutter] Dart 문법 (stream, async*, yield)
* stream : 파이프 개념. - 값, 이벤트, 객체, 컬렉션, 맵, 오류 또는 심지어 다른 스트림에서 모든 유형의 데이터가 스트림에 의해 전달 가능 - async* : 게으른 연산. 요청이 있을 때는 연산을 미루다
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/85
[Flutter] Dart 문법 (async, Future, await, then)
* 비동기 - 아래 printWithDelay함수에서 await를 빼고 실행시키면 Future.delayed와 print가 동시에 실행되는 듯 하고, await을 넣고 실행시키면 delay가 적용된 후에 print가 실행된다. //Future : An object representing
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/84
[Flutter] Dart 문법 (abstract, implement)
* 추상클래스, implement : 추상클래스 및 일반 클래스 implement 가능. - implement하면 클래스가 구현해 둔 메서드도 재정의 해야할 강제성이 생김. (일반 메서드도 구현해야 함) class Spacecraft extends Describ
cording-cossk3.tistory.com
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] Dart 문법(??=, ??, null 인식 연산자, cascade, ..) #flutter operater ?? #flutter null #dart .. (0) | 2021.03.16 |
---|---|
[Flutter] Dart 문법 (skip, take) (0) | 2021.03.15 |
[Flutter] Dart 문법 (getter, setter) (0) | 2021.03.15 |
[Flutter] Dart 문법 (async, Future, await, then) (0) | 2021.03.15 |
[Flutter] Dart 문법 (abstract, implement) (0) | 2021.03.15 |
댓글