Firebase
Firebase는 고품질 앱을 빠르게 개발하고 비즈니스를 성장시키는 데 도움이 되는 Google의 모바일 플랫폼입니다.
firebase.google.com
위 주소로 들어가면 파이어베이스가 나온다.
파이어베이스에서 프로젝트 등록을 다 끝냈다는 가정하에 글을 시작하겠다.
여러분은 개발자니까 여기까진 혼자힘으로 다 해결하실 수 있다고 생각한다.
메뉴에서 Firebase Database를 들어가면 FireStore가 나오고 시작하기를 누른다음 그냥 하라는대로 하면 셋팅은 끝난다.
위의 사진과 같이, FireStore는 기본적으로 Collection -> Document ->Data의 구조로 되어있다.
쉽게말해 컬렉션이라는 폴더안에 도큐멘트라는 문서가있고 그안에 JSON과 같은 형태의 Data가 들어있는 식이다.
또한 FireStore는 NoSQL을 사용한다.
나는 MySQL을 주로 사용을 했었지만, NoSQL을 처음 사용했을때도 크게 어렵지는 않았던것같다. 쉬운 쿼리만 했어서 그런지몰라도.
아무튼. FireStore로 Flutter에서 DB를 쏘고 받아오는 방법을 적겠다
회원정보를 FireStore에 쏜다는 가정을 하자.
class UserModel {
final String userKey;
final String profileImg;
final String email;
final List<dynamic> myPosts;
final int followers;
final List<dynamic> likedPosts;
final String userName;
final List<dynamic> followings;
final DocumentReference? reference;
대충 이런식으로 모델클래스를 만들어준다.
class UserModelState extends ChangeNotifier{
UserModel? _userModel;
UserModel get userModel=>_userModel!;
set userModel(UserModel userModel){
_userModel = userModel;
notifyListeners();
}
}
그리고 UserModel 클래스 정보를 변경, 얻어올수 있는 클래스도 생성해준다.
final DocumentReference userRef = FirebaseFirestore.instance.collection(COLLECTION_USERS)
.doc(userKey);
DocumentSnapshot snapshot = await userRef.get();
if(!snapshot.exists){
return await userRef.set(UserModel.getMapForCreateUser(email!));
}
그리고 이런식으로 DocumentReference에 본인이 원하는 경로를 지정해주고 (COLLECTION_USERS에 본인이 설정한 경로를 집어넣으면된다.) doc에는 원하는 도큐멘트 이름을 적으면 된다.
위의 메소드는 회원가입이기때문에 스냅샷을 얻어와서 스냅샷이 존재하지않을경우에만 데이터를 쓰는 식으로 짜주었다.
이런식으로 아주쉽게 FireStore에 데이터를 쓰고, 얻어올 수 있다.
'Flutter' 카테고리의 다른 글
flutter Google Map Marker에 이미지 파일 추가하기 (0) | 2022.07.31 |
---|---|
Could not find a file named "pubspec.yaml" When get from GitHub in Flutter (0) | 2022.06.08 |
Flutter Text 글자 수가 너무 길어져서 OverFlow가 난다면? (0) | 2022.05.01 |
[flutter] glassmorphism 을 사용해보자. (0) | 2021.08.18 |
[Flutter] GlobalKey란 무엇인가?? (0) | 2021.08.06 |