hacklink al hack forum organik hit kayseri escort b-ok zeta library books z libraryaskdozokxcaskdozokxcdeneme bonusu veren sitelerkumar sitelericasino metropolcasinomaxijojobetcasibomcasino sitelerisadsdasdaokadskozxcdonami bonasi voren satolitdonami bonasi voren satolitMovierulzcasino sitelericasibom girişcasibom girişcasibom giriştrendbetgrandpashabetpadişahbetjojobetholiganbetholiganbetmadridbetgrandpashabet girişholiganbet girişholiganbet giriş

Post Details Page

At Devhq.in, we provide comprehensive digital solutions for businesses. From software development and app creation to social media management and brand design, we help you build and grow your digital presence.

One to One Chat function in Flutter | How to Chat with Other with Firebase in flutter

One to One Chat function in Flutter | How to Chat with Other with Firebase in flutter

One to One Chat function in Flutter

If you want to communicate with other with the help of Flutter with firebase the you have to create a Model first that name is ChatModel.dart

This will help you to send a organize format of chat data and that is very helpful for send data in data base aur getting back from data base into app

For More information Click here : visit

Watch Youtube Video in Hindi https://devhq.in/building-an-advanced-chat-app-in-flutter-with-firebase-a-simple-guide-for-beginners-%e2%9c%a8/

📁ChatModel.dart


class ChatModel {
  String? id;
  String? message;
  String? senderName;
  String? senderId;
  String? receiverId;
  String? timestamp;
  String? readStatus;
  String? imageUrl;
  String? videoUrl;
  String? audioUrl;
  String? documentUrl;
  List<String>? reactions;
  List<dynamic>? replies;

  ChatModel({this.id, this.message, this.senderName, this.senderId, this.receiverId, this.timestamp, this.readStatus, this.imageUrl, this.videoUrl, this.audioUrl, this.documentUrl, this.reactions, this.replies});

  ChatModel.fromJson(Map<String, dynamic> json) {
    if(json["id"] is String) {
      id = json["id"];
    }
    if(json["message"] is String) {
      message = json["message"];
    }
    if(json["senderName"] is String) {
      senderName = json["senderName"];
    }
    if(json["senderId"] is String) {
      senderId = json["senderId"];
    }
    if(json["receiverId"] is String) {
      receiverId = json["receiverId"];
    }
    if(json["timestamp"] is String) {
      timestamp = json["timestamp"];
    }
    if(json["readStatus"] is String) {
      readStatus = json["readStatus"];
    }
    if(json["imageUrl"] is String) {
      imageUrl = json["imageUrl"];
    }
    if(json["videoUrl"] is String) {
      videoUrl = json["videoUrl"];
    }
    if(json["audioUrl"] is String) {
      audioUrl = json["audioUrl"];
    }
    if(json["documentUrl"] is String) {
      documentUrl = json["documentUrl"];
    }
    if(json["reactions"] is List) {
      reactions = json["reactions"] == null ? null : List<String>.from(json["reactions"]);
    }
    if(json["replies"] is List) {
      replies = json["replies"] ?? [];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["id"] = id;
    _data["message"] = message;
    _data["senderName"] = senderName;
    _data["senderId"] = senderId;
    _data["receiverId"] = receiverId;
    _data["timestamp"] = timestamp;
    _data["readStatus"] = readStatus;
    _data["imageUrl"] = imageUrl;
    _data["videoUrl"] = videoUrl;
    _data["audioUrl"] = audioUrl;
    _data["documentUrl"] = documentUrl;
    if(reactions != null) {
      _data["reactions"] = reactions;
    }
    if(replies != null) {
      _data["replies"] = replies;
    }
    return _data;
  }
}
One to one chat function in flutter

and also you need a ChatRoomModel

📁ChatRoomModel.dart

import 'package:sampark/Model/ChatModel.dart';
import 'package:sampark/Model/UserMode.dart';

class ChatRoomModel {
  String? id;
  UserModel? sender;
  UserModel? receiver;
  List<ChatModel>? messages;
  int? unReadMessNo;
  String? lastMessage;
  String? lastMessageTimestamp;
  String? timestamp;

  ChatRoomModel(
      {this.id,
      this.sender,
      this.receiver,
      this.messages,
      this.unReadMessNo,
      this.lastMessage,
      this.lastMessageTimestamp,
      this.timestamp});

  ChatRoomModel.fromJson(Map<String, dynamic> json) {
    if (json["id"] is String) {
      id = json["id"];
    }
    if (json["sender"] is Map) {
      sender =
          json["sender"] == null ? null : UserModel.fromJson(json["sender"]);
    }
    if (json["receiver"] is Map) {
      receiver = json["receiver"] == null
          ? null
          : UserModel.fromJson(json["receiver"]);
    }
    if (json["messages"] is List) {
      messages = json["messages"] ?? [];
    }
    if (json["unReadMessNo"] is int) {
      unReadMessNo = json["unReadMessNo"];
    }
    if (json["lastMessage"] is String) {
      lastMessage = json["lastMessage"];
    }
    if (json["lastMessageTimestamp"] is String) {
      lastMessageTimestamp = json["lastMessageTimestamp"];
    }
    if (json["timestamp"] is String) {
      timestamp = json["timestamp"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["id"] = id;
    if (sender != null) {
      _data["sender"] = sender?.toJson();
    }
    if (receiver != null) {
      _data["receiver"] = receiver?.toJson();
    }
    if (messages != null) {
      _data["messages"] = messages;
    }
    _data["unReadMessNo"] = unReadMessNo;
    _data["lastMessage"] = lastMessage;
    _data["lastMessageTimestamp"] = lastMessageTimestamp;
    _data["timestamp"] = timestamp;
    return _data;
  }
}

This will help you to send data into database and getting data from data base into app.

Then i create a function that will help to create a Room Id for two user communicate with us

⭐Create Room id Function

 String getRoomId(String targetUserId) {
    String currentUserId = auth.currentUser!.uid;
    if (currentUserId[0].codeUnitAt(0) > targetUserId[0].codeUnitAt(0)) {
      return currentUserId + targetUserId;
    } else {
      return targetUserId + currentUserId;
    }
  }

and with this function we will send message and receive message \

⭐ Send Message function

 Future<void> sendMessage(
      String targetUserId, String message, UserModel targetUser) async {
    isLoading.value = true;
    String chatId = uuid.v6();
    String roomId = getRoomId(targetUserId);
    var newChat = ChatModel(
      id: chatId,
      message: message,
      senderId: auth.currentUser!.uid,
      receiverId: targetUserId,
      senderName: controller.currentUser.value.name,
      timestamp: DateTime.now().toString(),
    );

    var roomDetails = ChatRoomModel(
      id: roomId,
      lastMessage: message,
      lastMessageTimestamp: DateTime.now().toString(),
      sender: controller.currentUser.value,
      receiver: targetUser,
      timestamp: DateTime.now().toString(),
      unReadMessNo: 0,
    );
    try {
      await db.collection("chats").doc(roomId).set(
            roomDetails.toJson(),
          );
      await db
          .collection("chats")
          .doc(roomId)
          .collection("messages")
          .doc(chatId)
          .set(
            newChat.toJson(),
          );
    } catch (e) {
      print(e);
    }
    isLoading.value = false;
  }

🔗Projects Resources

UI Figma File : Link

Github Code : Coming soon

Thanks for Reading 😍

Share now :

Facebook
Twitter
LinkedIn
Review Your Cart
0
Add Coupon Code
Subtotal