
글쓰기 버튼 클릭 함수
추후 controller.insert 결과에 따른 분기 추가
CustomElevatedButton(
  text: "글쓰기",
  funPageRoute: () {
    controller.insert(
    title: _title.text,
    content: _content.text,
    user: AuthController.to.firestoreUser()!);
  Get.to(() => HomePage());
}),Post model
import 'package:flutter_firebase_blog/domain/user/user.dart';
class Post {
  final String? title;
  final String? content;
  final UserModel? user;
  final DateTime? created;
  final DateTime? updated;
  Post({
    this.title,
    this.content,
    this.user,
    this.created,
    this.updated,
  });
  Post.fromJson(Map<String, dynamic> json)
      : title = json['title'],
        content = json['content'],
        user = UserModel.fromMap(json['user']),
        created = json['created'].toDate(),
        updated = json['updated'].toDate();
  Map<String, Object?> toJson() {
    return {
      'title': title,
      'content': content,
      'user': user!.toJson(),
      'created': created,
      'updated': updated,
    };
  }
}Post Controller
추후 insert 성공, 실패 분기 추가
  Future<void> insert({
    required String title,
    required String content,
    required UserModel user,
  }) async {
    await _postRepository.insert(title: title, content: content, user: user);
  }Post Provider
collection의 ref를 미리 가져와 add를 통해 insert
class PostProvider {
  final postRef = firebaseFirestore.collection("post").withConverter<Post>(
        fromFirestore: (snapshot, options) => Post.fromJson(snapshot.data()!),
        toFirestore: (post, options) => post.toJson(),
      );
  Future<void> insert(Post post) async => await postRef.add(post);
}Post Repository
  Future<void> insert({
    required String title,
    required String content,
    required UserModel user,
  }) async {
    Post post = Post(
      title: title,
      content: content,
      user: user,
      created: DateTime.now(),
      updated: DateTime.now(),
    );
    await _postProvider.insert(post);
  }Git Commit
https://github.com/mugon-dev/flutter_firebase_blog/commit/a76dbd077a45a5c828cba94f08a5b75045ab9c6b
글쓰기 구현 · mugon-dev/flutter_firebase_blog@a76dbd0
Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files 글쓰기 구현 Loading branch information Showing 12 changed files with 267 additions and 24 deletions. +20 −0 lib/
github.com
728x90
    
    
  'Project > flutter_firebase_blog' 카테고리의 다른 글
| 10. 전체 게시글 페이지 구현 (0) | 2021.07.28 | 
|---|---|
| 8. user update page 구현 (0) | 2021.07.28 | 
| 7. user info page 구현 (0) | 2021.07.26 | 
| 6. firebase auth, firebase cloud storage를 활용하여 google login 구현 (0) | 2021.07.26 | 
| 5. firebase auth, firebase cloud storage를 활용하여 email, password 로그인 구현 (0) | 2021.07.24 |