[Dart] Manipulate Lists/Arrays in Dart
2024-10-06 08:14:34
We will learn how to work with Lists using a variety of methods made available in the dart:core library. We will explore the top methods for working with List type collections.
Learn more about Lists at https://api.dartlang.org/stable/2.2.0/dart-core/List-class.html
Array to list:
because .map return iterable.
var fruits = ['banana', 'pineapple', 'orange', 'watermelon', 'apple'];
var fiboNumbers = [, , , , , , ];
List<Map<String, dynamic>> users = [
{ 'name': 'John', 'age': },
{ 'name': 'Jane', 'age': },
{ 'name': 'Mary', 'age': },
]; // array to list
var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList();
print(mappedFruits);
reduce vs fold:
reduce: doesn't provide the init value, but 'fold' does:
const initialValue = ;
var sum2 = fiboNumbers.fold(initialValue, (curr, next) => curr + next);
print( sum2 ); var sum = fiboNumbers.reduce((curr, next) => curr + next);
print( sum );
filtering:
var over21s = users.where((user) => user['age'] > );
print( over21s.length ); var nameJ = users.firstWhere((user) => user['name'].startsWith('J'), orElse: () => null);
print( nameJ ); var under18 = users.singleWhere((user) => user['age'] < , orElse: () => {'error': 'Not Found'});
print( under18 );
take & skip:
print( fiboNumbers.take().toList() );
print( fiboNumbers.skip().toList() );
print( fiboNumbers.take().skip().take().toList() );
expend: the same as flatMap in JS
var flattened = [[, ], [, ]].expand((pair) => pair).toList();
print( flattened ); var duplicated = fiboNumbers.expand((i) => [i, i]).toList();
print( duplicated );
最新文章
- iOS监听tableView组头切换事件
- vue中v-bind:class动态添加class
- C#正则表达式Regex常用匹配
- C# 通过代理获取url数据
- 《CSS3实战》读书笔记 第三章:选择器:样式实现的标记
- Ruiy自我识人做事领悟录ing
- EAI概述
- oracle服务介绍
- PAT---1005. Spell It Right (20)
- JS学习之prototype属性
- 如何利用【百度地图API】进行定位?非GPS定位
- 【JAVA笔记】JAVA后端实现统一扫码支付:微信篇
- 小练习,判断X的奇偶性
- (2018干货系列三)最新PHP学习路线整合
- KVM环境安装macOS Sierra
- python之属性描述符与属性查找规则
- 在window下安装第二个mysql
- 很简单的在Ubuntu系统下安装字体和切换默认字体的方法
- laravel5的Bcrypt加密方式对系统保存密码的小结
- QT的信号和槽机制简介
热门文章
- SQL语言(二)
- django使用pyecharts(5)----django加入echarts_增量更新_定长
- WUSTOJ 1337: Car race game(C)树状数组,离散化
- matplotlib实例笔记
- go select 的default
- 使用 react 的 hooks 进行全局的状态管理
- 解决阿里云OSS The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint的办法
- 使用Harbor搭建Docker私有镜像仓库
- 从零开始搭建一个简单的基于webpack的vue开发环境
- 数据结构之队列(queue)