Stack
Divider
AnimatedOpacity
AnimatedContainer
AspectRatio
Wrap
cached_network_image: ^3.3.0
dio: ^5.3.3
intl: ^0.18.1
shared_preferences: ^2.2.1
url_launcher: ^6.1.14
상태 초기화. 지난번에 어쩌다가 쓸 일이 있게 되어서 썼던 건데, 오늘에서야 뭔지 배우게 되었다.
useEffect에서 의존성배열에 빈배열([]) 준거랑 똑같다.
처음 렌더링될때만 실행된다.
기존에 day6날에 했던 과제를 고대로 들고와서 디벨롭(?)했다.
List<String>? cart = [];
SharedPreferences? prefs; //타입을 정확히 하면 익스텐션이 더 많은 도움을 준다.
void initState() { //상태 초기화
super.initState();
initPreferences();
}
void initPreferences() async {
prefs = await SharedPreferences.getInstance(); //
if (prefs != null) {
cart = prefs!.getStringList('cart');
}
}
어떻게 저떻게 하기보다 그냥 애초에 배열을 가지고 add, remove를 기존에 하는 함수가 있기 때문에 함수가 실행되면 동기화만 시켜주었다. 굳이 관리중인 배열이 있는데 또 따로 prefs.set어쩌구 이런거 할 필요가 없어보여서.
void addMenu(String item) {
setState(() {
cart?.add(item);
});
if (prefs != null) {
prefs?.setStringList('cart', cart!); //동기화
}
}
void deleteMenu(String item) {
setState(() {
cart?.remove(item);
});
if (prefs != null) {
prefs?.setStringList('cart', cart!); //동기화
}
}
class User {
String userId;
String nickname;
DateTime created;
DateTime updated;
DateTime visited;
List<User> followers;
List<User> followings;
List<User> blocked;
List<Feed> feeds;
}
List<Map<String, String>> words = [
{
"word": "resilient",
"meaning": "탄력있는, 회복력있는",
"example": "She's a resilient girl"
},
{
"word": "revoke",
"meaning": "취소하다",
"example":
"The authorities have revoked their original decision to allow development of this rural area."
},
];
class Word {
String word;
String meaning;
String example;
Word({required this.word,
required this.meaning,
required this.example,
});
}
Class = {
- 멤버변수
- 멤버함수(메서드)
- 생성자
}
클래스 생성자(Constructor)
객체 생성하는 함수. 생성자함수라고도 함예시1: 이름 없는 생성자(unnamed)
class User { final name; User(this.name); }
예시2: 이름있는 생성자(named)
class User { String name; User.withAnonymous() : name = '익명'; } void main() { var user = User.withAnonymous(); //익명 유저 생성 가능 }
- required, null safety 모두 활용 가능.
- {} 를 사용할 수도 있다.(optional)
서버로부터 받은 객체 Map으로 바꿔서 클래스로 생성한다.
var word = Word.fromMap(networkData);
서버에 Map 객체 전달
Map<String, dynamic> toMap() => {
'word': word,
'meaning': meaning,
'example': example,
}
word.toMap();
다른 타입 String으로 바꾸고 싶을 때 주로 사용. Text 위젯에 데이터 넣을 때 자주 사용함.
class들은 Obejct의 자식임. Object의 메서드를 상속받아서 사용할 수 있게 됨.
override를 통해 class에 적용된 부모의 기능을 재정의 할 수 있다.
class Word {
String toString() {
return "Word($word)";
}
}
변수나 객체는 각각 다른 고유의 hashCode값을 가진다.
//부모 기능을 재정의함
operator ==(Object other) {
if (other is Word) {
return word == other;
}
return false;
}
print("apple" == Word); //Word 클래스로 만들어진 객체가 맞는지
class에서 변수를 update할때 사용하는 함수