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

相关推荐
小风呼呼吹儿7 小时前
Flutter 框架跨平台鸿蒙开发 - 车辆保养记录器:智能管理车辆保养全流程
flutter·华为·harmonyos
不会写代码0008 小时前
Flutter 框架跨平台鸿蒙开发 - 在线小说阅读器开发教程
flutter·华为·harmonyos
2501_944521598 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
ujainu8 小时前
Flutter for HarmonyOS 前置知识:Dart语言详解(中)
flutter
2501_944521599 小时前
Flutter for OpenHarmony 微动漫App实战:图片加载实现
android·开发语言·前端·javascript·flutter·php
新镜10 小时前
【Flutter】LTR/RTL 阿拉伯语言/希伯来语言
android·flutter·ios·客户端
夜雨声烦丿14 小时前
Flutter 框架跨平台鸿蒙开发 - 万年历应用开发教程
flutter·华为·harmonyos
别退15 小时前
flutter_gradle_android
android·flutter
2501_9444241216 小时前
Flutter for OpenHarmony游戏集合App实战之黑白棋落子翻转
android·开发语言·windows·flutter·游戏·harmonyos
猛扇赵四那边好嘴.16 小时前
Flutter 框架跨平台鸿蒙开发 - 全国公厕查询:智能定位附近公厕
flutter·华为·harmonyos