flutter对数组中某个数据二次加工成单独的数组

如何将数据2,1,2,2,2,1,2,2,3,2,2,2,2,3,2,2,2,2,2,3,2,4,2,2,1,2,3,2,4,2加工成

\[2, 1, 2, 2, 2, 1, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 4, 2, 2, 1, 2, 3, 2, 4, 2]。这是实际工作中遇到的问题,UI要求将某一类型数据(如上述数组中的2)用宫格(GridView)展示,其他类型数据用列表(ListView)展示,整体就是一个ListView嵌套很多GridView。

代码运行结果:

​​​​​​​

完整代码:

Dart 复制代码
Future<void> main(List<String> args) async {
  List<int> intList = [2,1,2,2,2,1,2,2,3,2,2,2,2,3,2,2,2,2,2,3,2,4,2,2,1,2,3,2,4,2];
  arrayProcess(intList,2);
}
///intList,加工的数组
///加工的数据
List<Object> arrayProcess(List<int> intList,int num){
  List<Object> ints = [];
  int times = 1;
  List<int> temp = [];
  for(int i=0;i<intList.length;i++){
    if(i < (intList.length -1)){
      if(intList[i]==intList[i+1] && intList[i]==num){
        times= times+1;
        temp.add(intList[i]);
      }else {
        if(times>1){
          if(temp.length>0){
            temp.add(intList[i]);
            List<int> _temp = [];
            temp.forEach((element) {
              _temp.add(element);
            });
            ints.add(_temp);
            temp.clear();
          }

          times = 1;
        }
        else{
          if(intList[i]==num){
            temp.add(intList[i]);
            List<int> _temp = [];
            temp.forEach((element) {
              _temp.add(element);
            });
            ints.add(_temp);
            temp.clear();
          }
          else{
            ints.add(intList[i]);
            times = 1;
          }
        }
      }
    }
    else{
      if(times>1){
        if(temp.length>0){
          temp.add(intList[i]);
          List<int> _temp = [];
          temp.forEach((element) {
            _temp.add(element);
          });
          ints.add(_temp);
          temp.clear();
        }

        times = 1;
      }
      else{
        if(intList[i]==num){
          temp.add(intList[i]);
          List<int> _temp = [];
          temp.forEach((element) {
            _temp.add(element);
          });
          ints.add(_temp);
          temp.clear();
        }
        else{
          ints.add(intList[i]);
          times = 1;
        }
      }
    }
  }
  print(ints.toString());
  return ints;
}
相关推荐
程序员老刘5 小时前
跨平台开发地图 | 2026年9月
flutter·客户端
HouWan8 小时前
Flutter iOS UISceneDelegate 迁移指南:理清新的 Scene 生命周期
flutter·ios·app
技术任我行XTing11 小时前
【DFX系列】Flutter 鸿蒙应用外接纹理介绍及问题定位
flutter·harmonyos
恋猫de小郭13 小时前
Flutter GSoC 2026 提案进度解读,补上 DevTools、FFI 和原生平台的关键缺口
android·前端·flutter
●VON1 天前
Flutter 鸿蒙插件适配实战:用 flutter_native_timezone_2025 1.0.1 读取当前时区与系统目录
flutter·华为·harmonyos·鸿蒙
黑科技iOS上架1 天前
iOS深度混淆flutter应用的最佳实践
flutter·ios·混淆·审核·深度混淆
恋猫de小郭2 天前
CPF-Flutter 社区提出折叠场景分栏(平行视界) 方案
android·前端·flutter
程序员老刘2 天前
Android Studio Quail 4发布,看日志我以为谷歌放弃Flutter了
flutter·android studio·ai编程
Crazy_MT2 天前
Flutter 本地大模型实战:做一个自然语言记账工具
flutter·llm·ai编程