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; } ```

相关推荐
maaath12 分钟前
【maaath】Flutter for OpenHarmony 实战:旅游攻略应用开发指南
flutter·华为·harmonyos
jiejiejiejie_1 小时前
Flutter for OpenHarmony 跨平台开发:计算器功能实战指南
flutter
jiejiejiejie_2 小时前
Flutter for OpenHarmony 交互体验实战合集:底部导航优化 + 萌系用户反馈全攻略
flutter
liulian09162 小时前
Flutter for OpenHarmony 跨平台开发:番茄钟功能实战指南
flutter
liulian09163 小时前
Flutter for OpenHarmony 效率工具开发实战:我实现的番茄钟与倒计时功能总结
flutter
jiejiejiejie_4 小时前
Flutter for OpenHarmony 跨平台开发:待办事项功能实战指南
flutter
maaath5 小时前
【maaath】Flutter for OpenHarmony 实战:电影榜单应用开发指南
flutter·华为·harmonyos
xmdy58668 小时前
Flutter+开源鸿蒙实战|智安盾电商溯源平台Day6 登录逻辑+积分体系+全局收尾优化
flutter·华为·harmonyos
liulian09169 小时前
Flutter for OpenHarmony 工具类应用实战总结:计算器 + 记事本功能开发全解析
flutter
911hzh10 小时前
Flutter WebRTC iOS 原理解析:从 getUserMedia 到 Texture,讲清视频采集、纹理渲染与远端通话链路
flutter·ios·webrtc