• Flutter高仿微信-第59篇-同步数据


     Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

     详情请查看

    实现代码:

    //服务器同步数据
    Future syncFirst(String account, String packageName) async {
      Response result = await HttpUtils.getInstance().syncFirst(account, packageName);
      LogUtils.d("syncFirst返回数据:${result.toString()}");
      final responseResult = jsonDecode(result.toString());
      final baseResult = BaseResult.fromJson(responseResult);
      //LogUtils.d("syncFirst返回baseResult数据:${baseResult}");
      final syncFirstJson = jsonDecode(baseResult.data.toString());
      SyncFirstBean syncFirstBean = SyncFirstBean.fromJson(syncFirstJson);
      List syncUser = syncFirstBean.syncUser;
      List syncContacts = syncFirstBean.syncContacts;
      List syncChat = syncFirstBean.syncChat;
      List syncGroup = syncFirstBean.syncGroup;
      List syncGroupUser = syncFirstBean.syncGroupUser;
    
      processSyncUser(syncUser);
      processSyncContact(syncContacts);
      processSyncChat(syncChat);
      processSyncGroup(syncGroup);
      processSyncGroupUser(syncGroupUser);
      return true;
    }

    //处理登录账户
    void processSyncUser(List syncUser) async{
      List resultUserList = [];
      for(int i = 0; i < syncUser.length; i++){
        UserBean userBean = syncUser[i];
        //LogUtils.d("第一次同步用户:${userBean.account}, ${userBean.nickName} , ${userBean.address},  ${userBean.note}");
        UserBean? localUserBean = await userDao?.findUserByAccount(userBean.account!);
        if(localUserBean == null){
          resultUserList.add(userBean);
        }
      }
      userDao?.insertUserList(resultUserList);
    }
    
    //处理联系人
    void processSyncContact(List syncContacts) async{
      /*syncContacts.forEach((contactsBean) {
        LogUtils.d("第一次同步联系人:${contactsBean.fromAccount}, ${contactsBean.toAccount} , ${contactsBean.type}");
      });*/
    
      List resultContactsList = [];
      for(int i = 0; i < syncContacts.length; i++){
        ContactsBean contactsBean = syncContacts[i];
        ContactsBean? localContactsBean = await contactsDao?.findContactByFromOrToAccount(contactsBean.fromAccount??"", contactsBean.toAccount??"");
        //如果已经存在联系人, 不需要重复插入
        if(localContactsBean == null){
          resultContactsList.add(contactsBean);
        }
      }
    
      contactsDao?.insertContactList(resultContactsList);
    }
    
    //处理聊天信息
    void processSyncChat(List syncChat){
      /*syncChat.forEach((chatBean) {
        LogUtils.d("第一次同步聊天信息:${chatBean.fromAccount}, ${chatBean.toAccount} ${chatBean.content}");
      });*/
      ChatDao? chatDao = DBUtils.getInstance().getDatabase()?.chatDao;
      chatDao?.insertChatList(syncChat);
    }
    
    //处理群名信息
    void processSyncGroup(List syncGroup){
      GroupRepository.getInstance().insertGroupList(syncGroup);
    }
    
    //处理群成员信息
    void processSyncGroupUser(List syncGroupUser){
      GroupUserRepository.getInstance().insertGroupUserList(syncGroupUser);
    }
  • 相关阅读:
    微服务项目:尚融宝(32)(后端搭建:会员列表搭建(3))
    Android 漏洞修复
    【Spring Boot 集成应用】Kafka的集成用法
    Effective Modern C++[实践]->auto类型推导不符合要求时,使用强转
    关于如何找环形链表的入环点
    HTTP协议
    微信小程序 vantweapp里面的上传upload组件研究
    训练千亿参数大模型,离不开四种GPU并行策略
    【TIPs】 Visual Stadio 2019 中本地误使用“git的重置 - 删除更改 -- hard”后,如何恢复?
    在线手写汉字
  • 原文地址:https://blog.csdn.net/maoning20080808/article/details/128051350