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

相关推荐
bu_xue2 小时前
【LiveStates 01】别再手动 watch 了:开启 Flutter “自动追踪” DX 革命
flutter
bu_xue3 小时前
【LiveStates 05】实战指南:手把手带你用 LiveStates 构建高性能生产级页面
flutter
程序员老刘4 小时前
AI写Flutter代码比我快100倍,我慌了吗?
flutter·ai编程·客户端
SY.ZHOU6 小时前
大型工程跨全平台实践总结
flutter·ios·安卓·鸿蒙
weixin_443478519 小时前
Flutter学习之导航与路由
java·学习·flutter
恋猫de小郭10 小时前
Flutter 鸿蒙 2026 路线发布,加速同步官方生态,进一步优化体验
前端·flutter·harmonyos
亚历克斯神11 小时前
Flutter 三方库 fft 的鸿蒙化适配指南 - 实现端侧高性能快速傅里叶变换、支持音频频谱分析与信号处理域的频域特征提取实战
flutter·harmonyos·鸿蒙·openharmony
鹏多多12 小时前
Flutter使用pretty_qr_code生成高颜值二维码
android·前端·flutter
不爱吃糖的程序媛1 天前
Flutter 3.35.7-ohos-0.0.3 发布:能力增强、性能优化与多项问题修复
flutter
始持1 天前
第三讲 进阶布局与样式(精细化UI)
flutter