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。 代码运行结果: ![](https://file.jishuzhan.net/article/1694500807568789506/7a2c55a934ba4d3e93ae0c0b651a3f8c.png)​​​​​​​ 完整代码: ```Dart Future main(List args) async { List 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 arrayProcess(List intList,int num){ List ints = []; int times = 1; List temp = []; for(int i=0;i1){ if(temp.length>0){ temp.add(intList[i]); List _temp = []; temp.forEach((element) { _temp.add(element); }); ints.add(_temp); temp.clear(); } times = 1; } else{ if(intList[i]==num){ temp.add(intList[i]); List _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 _temp = []; temp.forEach((element) { _temp.add(element); }); ints.add(_temp); temp.clear(); } times = 1; } else{ if(intList[i]==num){ temp.add(intList[i]); List _temp = []; temp.forEach((element) { _temp.add(element); }); ints.add(_temp); temp.clear(); } else{ ints.add(intList[i]); times = 1; } } } } print(ints.toString()); return ints; } ```

相关推荐
卢叁14 分钟前
Flutter之自定义TabIndicator
前端·flutter
萧雾宇2 小时前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
拜无忧2 小时前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧3 小时前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio
醉过才知酒浓4 小时前
flutter 拦截返回按钮的方法(WillPopScope or PopScope)
flutter
傅里叶5 小时前
sudo启动Flutter程序AMD初始化失败
linux·flutter
苦逼的搬砖工6 小时前
Flutter UI Components:闲来无事,设计整理了这几年来使用的UI组件库
前端·flutter
黑金IT8 小时前
Dart → `.exe`:Flutter 桌面与纯命令行双轨编译完全指南
flutter
iOS_MingXing8 小时前
flutter TabBar 设置isScrollable 第一个有间距
flutter
小红星闪啊闪1 天前
Flutter开发 -- 需要了解的Dart知识
flutter