본문 바로가기

Flutter/12 Clone 당근마켓

[Flutter] Clone - 당근마켓48(chatroomModel, chatModel)

이번에는 채팅창을 구현하기 위한 채팅룸과 각 채팅메시지에 대한 모델링 구현해 보겠습니다.

여기서는 "https://app.quicktype.io" 을 이용하여 모델을 구현했습니다.

개발환경 : 윈도우11, 안드로이드 스튜디오, flutter 3.0.1

 

 

"https://app.quicktype.io" 을 이용한 모델링 방법은 아래 링크를 참고하세요

2022.06.30 - [Flutter/04 Widgets] - [Flutter] Widgets - Google map 2

 

[Flutter] Widgets - Google map 2

이번 카테고리는 google map 을 사용하는 방법에 대해서 알아보겠습니다. 개발환경 : 윈도우11, 안드로이드 스튜디오, flutter 3.0.1 소스코드 위치 - https://github.com/mike-bskim/google_map_test/releases/t..

unsungit.tistory.com

 

 

 

"Json to Dart" 을 이용한 모델링 방법은 아래 링크를 참고하세요

2022.04.28 - [Flutter/10 app Weather] - [Flutter] App Weather - 3.5단계 model of openweathermap

 

[Flutter] App Weather - 3.5단계 model of openweathermap

이번에는 데이터 모델을 만드는 방법에 대해서 알아보겠습니다. 개발환경 : 윈도우11, 안드로이드 스튜디오(Arctic Fox 2020.3.1 Patch 4), flutter 2.8.1 소스코드 위치 - Release data_model · mike-bskim/wea..

unsungit.tistory.com

 

 

 

./src/models/chatroom_model.dart

 

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:geoflutterfire/geoflutterfire.dart';

import '../constants/data_keys.dart';

/// "item_image" : "item_image",
/// "item_title" : "item_title",
/// "item_key" : "item_key",
/// "item_address" : "item_address",
/// "item_price" : 0.0,
/// "seller_key" : "seller_key",
/// "buyer_key" : "buyer_key",
/// "seller_image" : "seller_image",
/// "buyer_image" : "buyer_image",
/// "geo_fire_point" : "geo_fire_point",
/// "last_msg" : "last_msg",
/// "last_msg_time" : "",
/// "last_msg_user_key" : "last_msg_user_key",
/// "chatroom_key" : "chatroom_key"

// To parse this JSON data, do
//
//     final chatroomModel2 = chatroomModel2FromJson(jsonString);

import 'dart:convert';

ChatroomModel2 chatroomModel2FromJson(String str) => ChatroomModel2.fromJson(json.decode(str));

String chatroomModel2ToJson(ChatroomModel2 data) => json.encode(data.toJson());

class ChatroomModel2 {
  String itemImage;
  String itemTitle;
  String itemKey;
  String itemAddress;
  num itemPrice;
  String sellerKey;
  String buyerKey;
  String sellerImage;
  String buyerImage;
  GeoFirePoint geoFirePoint;
  String lastMsg;
  DateTime lastMsgTime;
  String lastMsgUserKey;
  String chatroomKey;
  DocumentReference? reference;

  ChatroomModel2({
    required this.itemImage,
    required this.itemTitle,
    required this.itemKey,
    required this.itemAddress,
    required this.itemPrice,
    required this.sellerKey,
    required this.buyerKey,
    required this.sellerImage,
    required this.buyerImage,
    required this.geoFirePoint,
    required this.lastMsg,
    required this.lastMsgTime,
    required this.lastMsgUserKey,
    required this.chatroomKey,
    this.reference,
  });

  factory ChatroomModel2.fromJson(Map<String, dynamic> json) => ChatroomModel2(
      itemImage: json[DOC_ITEMIMAGE] ?? '',
      itemTitle: json[DOC_ITEMTITLE] ?? '',
      itemKey: json[DOC_ITEMKEY] ?? '',
      itemAddress: json[DOC_ITEMADDRESS] ?? '',
      itemPrice: json[DOC_ITEMPRICE] ?? 0,
      sellerKey: json[DOC_SELLERKEY] ?? '',
      buyerKey: json[DOC_BUYERKEY] ?? '',
      sellerImage: json[DOC_SELLERIMAGE] ?? '',
      buyerImage: json[DOC_BUYERIMAGE] ?? '',
      geoFirePoint: json[DOC_GEOFIREPOINT] == null
          ? GeoFirePoint(0, 0)
          : GeoFirePoint((json[DOC_GEOFIREPOINT][DOC_GEOPOINT]).latitude,
              (json[DOC_GEOFIREPOINT][DOC_GEOPOINT]).longitude),
      lastMsg: json[DOC_LASTMSG] ?? '',
      lastMsgTime: json[DOC_LASTMSGTIME] == null
          ? DateTime.now().toUtc()
          : (json[DOC_LASTMSGTIME] as Timestamp).toDate(),
      lastMsgUserKey: json[DOC_LASTMSGUSERKEY] ?? '',
      chatroomKey: json[DOC_CHATROOMKEY] ?? '');

  Map<String, dynamic> toJson() => {
        DOC_ITEMIMAGE : itemImage,
        DOC_ITEMTITLE : itemTitle,
        DOC_ITEMKEY : itemKey,
        DOC_ITEMADDRESS : itemAddress,
        DOC_ITEMPRICE : itemPrice,
        DOC_SELLERKEY : sellerKey,
        DOC_BUYERKEY : buyerKey,
        DOC_SELLERIMAGE : sellerImage,
        DOC_BUYERIMAGE : buyerImage,
        DOC_GEOFIREPOINT : geoFirePoint.data,
        DOC_LASTMSG : lastMsg,
        DOC_LASTMSGTIME : lastMsgTime,
        DOC_LASTMSGUSERKEY : lastMsgUserKey,
        DOC_CHATROOMKEY : chatroomKey,
      };

  static String generateChatRoomKey({required String buyer, required String itemKey}) {
    return '${itemKey}_$buyer';
  }

}

 

 

 

./src/models/chat_model.dart

 

import 'package:cloud_firestore/cloud_firestore.dart';

import '../constants/data_keys.dart';

/// "chatKey" : "",
/// "msg" : "",
/// "createDate" : "",
/// "userKey" : "",
/// "reference" : ""

// To parse this JSON data, do
//
//     final chatModel2 = chatModel2FromJson(jsonString);

import 'dart:convert';

ChatModel2 chatModel2FromJson(String str) => ChatModel2.fromJson(json.decode(str));

String chatModel2ToJson(ChatModel2 data) => json.encode(data.toJson());

class ChatModel2 {
  String chatKey;
  String msg;
  String createdDate;
  String userKey;
  DocumentReference? reference;

  ChatModel2({
    required this.chatKey,
    required this.msg,
    required this.createdDate,
    required this.userKey,
    this.reference,
  });

  factory ChatModel2.fromJson(Map<String, dynamic> json) => ChatModel2(
    chatKey: json[DOC_CHATKEY],
    msg: json[DOC_MSG],
    createdDate: json[DOC_CREATEDDATE],
    userKey: json[DOC_USERKEY],
    // reference: json["reference"],
  );

  Map<String, dynamic> toJson() => {
    DOC_CHATKEY: chatKey,
    DOC_MSG: msg,
    DOC_CREATEDDATE: createdDate,
    DOC_USERKEY: userKey,
    // "reference": reference,
  };
}