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

相关推荐
勤劳打代码1 小时前
触类旁通 —— Flutter 与 React 对比解析
前端·flutter·react native
消失的旧时光-194317 小时前
Flutter Event Loop
flutter
程序员老刘18 小时前
跨平台开发地图:客户端技术选型指南 | 2025年10月
flutter·react native·客户端
傅里叶19 小时前
Flutter 工程环境、插件使用、protobuf配置与字体/持久化管理
flutter
傅里叶19 小时前
Flutter之《环境与依赖配置》
flutter
大雷神19 小时前
【成长纪实】HarmonyOS中ArkTS与Flutter数据类型对比详解
flutter
未来猫咪花1 天前
Riverpod 3.0:一个过度设计的反面教材
flutter
消失的旧时光-19431 天前
Flutter 并发编程全解:从零掌握 Isolate
flutter
西西学代码1 天前
Flutter---EQ均衡器
flutter
LinXunFeng2 天前
Flutter webview 崩溃率上升怎么办?我的分析与解决方案
flutter·ios·webview