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

相关推荐
瓜子三百克3 小时前
七、性能优化
flutter·性能优化
恋猫de小郭10 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
小蜜蜂嗡嗡17 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
瓜子三百克21 小时前
十、高级概念
flutter
帅次1 天前
Objective-C面向对象编程:类、对象、方法详解(保姆级教程)
flutter·macos·ios·objective-c·iphone·swift·safari
小蜜蜂嗡嗡1 天前
flutter flutter_vlc_player播放视频设置循环播放失效、初始化后获取不到视频宽高
flutter
孤鸿玉1 天前
[Flutter小技巧] Row中widget高度自适应的几种方法
flutter
bawomingtian1231 天前
FlutterView 源码解析
flutter
Zender Han2 天前
Flutter 进阶:实现带圆角的 CircularProgressIndicator
flutter