flutter 写一个通用方法,通过回调返回数据

//先获取数据,设置回调

c 复制代码
  ///获取好友资料
  getUserArchivesInfo(int? userId,
      {Function(bool isCache, ArchivesInfo archivesInfo)? onSuccess,
      Function(String error)? onError}) {
    RxHttp<ArchivesInfo>()
      ..init()
      ..setBaseUrl(Api.BASE_API)
      ..setPath(Api.GET_OTHER_ARCHIVES_INFO)
      ..setCacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
      ..setParams({"user_id": userId})
      ..setJsonTransFrom((data) {
        PrintUtil.prints('$TAG 获取好友资料 ===> $data');
        return ArchivesInfo.fromJson(json.decoder.convert(data)['data']);
      })
      ..call(
          NetCallback(onNetFinish: (archivesInfo) {
            if (onSuccess != null) {
              CacheFileString().loadStringAsync(archivesInfo.avatar ?? '');
              CacheFileString().loadStringAsync(archivesInfo.avatarThumb ?? '');
              archivesInfo.userIMID = '${archivesInfo.userID}';
              archivesInfo.conversationID = 'c2c_${archivesInfo.userID}';
              onSuccess(false, archivesInfo);
            }
          }, onCacheFinish: (archivesInfo) {
            if (onSuccess != null) {
              onSuccess(true, archivesInfo);
            }
          }, onNetError: (errorCode, error) {
            if (onError != null) {
              onError('$error ($errorCode)');
            }
          }),
          server: Servers.microServices);
  }

//在回调处处理数据和刷新页面

c 复制代码
  ///获取好友的档案资料
  _getOtherArchivesInfo() {
    WsImUserInfoUtil().getUserArchivesInfo(int.parse(userID),
        onSuccess: (isCache, archivesInfo) {
      if (!isCache) {
        WsImDBUtil().updateUserInfo('c2c_$userID', archivesInfo, false);
      }
      print('回调了多少次');
      setOtherArchivesInfo(archivesInfo);
    }, onError: (error) {
      showToast(error);
    });
  }

//处理数据的方法

c 复制代码
setOtherArchivesInfo(data) {
    mySetState(() {
      // archivesInfo = ArchivesInfo.fromJson(data);
      archivesInfo = data;
      String photoIds = '';
      String videoIds = '';

      if (archivesInfo != null) {
        if (archivesInfo!.photos != null) {
          for (int i = 0; i < archivesInfo!.photos!.length; i++) {
            if (i == archivesInfo!.photos!.length - 1) {
              photoIds += '${archivesInfo!.photos![i].photoId}';
            } else {
              photoIds += '${archivesInfo!.photos![i].photoId},';
            }
          }
        }

        if (archivesInfo!.videos != null) {
          for (int i = 0; i < archivesInfo!.videos!.length; i++) {
            if (i == archivesInfo!.videos!.length - 1) {
              videoIds += '${archivesInfo!.videos![i].videoId}';
            } else {
              videoIds += '${archivesInfo!.videos![i].videoId},';
            }
          }
        }
      }

      if (photoIds != '') {
        _getPhotoState(archivesInfo!.photos!, photoIds);
      }

      if (videoIds != '') {
        _getVideoState(archivesInfo!.videos!, videoIds);
      }
    });
  }
相关推荐
旷世奇才李先生几秒前
Java 内置HttpClient 深度实战与性能优化全指南
java
西贝爱学习5 分钟前
pdf转TXT文本,适用于文字型PDF;扫描版PDF需要使用OCR(光学字符识别)技术来识别图中的文字
java·服务器·前端
ZC跨境爬虫8 分钟前
跟着 MDN 学 HTML day_43:(DocumentFragment 接口详解)
前端·javascript·vue.js·ui·html·音视频
xmdy586616 分钟前
Flutter + 开源鸿蒙实战|城市智慧停车管理系统 Day6 全局组件封装+意见反馈+系统设置+代码重构+bug修复+细节调优
flutter·开源·harmonyos
青柠代码录17 分钟前
【JVM】面试题-Java中有哪些引用类型
java·jvm
Bigger19 分钟前
mini-cc:用最小的代码,复刻一个“真正能干活”的 AI 编程智能体(并且把架构讲清楚)
前端·ai编程·claude
问心无愧051322 分钟前
ctf show web 入门46
android·前端·笔记
计算机安禾23 分钟前
【c++面向对象编程】第7篇:static成员:属于类而不是对象的变量和函数
java·c++·算法
xmdy586624 分钟前
Flutter + 开源鸿蒙实战|城市智慧停车管理系统 Day5 超时提醒弹窗+深浅色全局主题+登录页面+记住登录+隐私协议+全局UI美化+鸿蒙打包配置
flutter·开源·harmonyos