Flutter框架适配鸿蒙:Collection集合方法
·




一、集合方法概述
Dart提供丰富的集合操作方法,使数据处理更简洁高效。
|| 方法 | 功能 | 返回类型 |
|------|------|----------|
| map | 转换元素 | Iterable |
| where | 过滤元素 | Iterable |
| fold | 聚合计算 | T |
| reduce | 归约计算 | T |
| sort | 原地排序 | void |
二、示例代码
class _Page09CollectionDemo extends StatefulWidget {
const _Page09CollectionDemo();
State<_Page09CollectionDemo> createState() => _Page09CollectionDemoState();
}
class _Page09CollectionDemoState extends State<_Page09CollectionDemo> {
final List<int> _numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
String _result = '';
void _filterEven() {
setState(() {
final evenNumbers = _numbers.where((n) => n % 2 == 0).toList();
_result = '偶数: ${evenNumbers.join(', ')}';
});
}
void _squareAll() {
setState(() {
final squares = _numbers.map((n) => n * n).toList();
_result = '平方: ${squares.join(', ')}';
});
}
void _sumAll() {
setState(() {
final sum = _numbers.fold(0, (acc, n) => acc + n);
_result = '总和: $sum';
});
}
void _sortDesc() {
setState(() {
final sorted = List.from(_numbers)..sort((a, b) => b.compareTo(a));
_result = '降序: ${sorted.join(', ')}';
});
}
Widget build(BuildContext context) {
return Container(
color: Colors.indigo.shade50,
padding: const EdgeInsets.all(20),
child: Column(
children: [
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.indigo.shade600,
borderRadius: BorderRadius.circular(20),
),
child: const Column(
children: [
Icon(Icons.data_array, size: 48, color: Colors.white),
SizedBox(height: 16),
Text(
'集合操作',
style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.white),
),
SizedBox(height: 8),
Text('数据转换 - 页面 9/10', style: TextStyle(color: Colors.white70)),
],
),
),
const SizedBox(height: 24),
Expanded(
child: Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20)),
child: Column(
children: [
Text(
_numbers.join(', '),
style: const TextStyle(fontSize: 16, color: Colors.grey),
),
const SizedBox(height: 20),
if (_result.isNotEmpty)
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.indigo.shade100,
borderRadius: BorderRadius.circular(10),
),
child: Text(_result, style: const TextStyle(fontSize: 18)),
),
const Spacer(),
Column(
children: [
ElevatedButton(
onPressed: _filterEven,
style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo.shade600),
child: const Text('过滤偶数', style: TextStyle(color: Colors.white)),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _squareAll,
style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo.shade600),
child: const Text('平方计算', style: TextStyle(color: Colors.white)),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _sumAll,
style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo.shade600),
child: const Text('求和', style: TextStyle(color: Colors.white)),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _sortDesc,
style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo.shade600),
child: const Text('降序排列', style: TextStyle(color: Colors.white)),
),
],
),
],
),
),
),
],
),
);
}
}
三、常用方法
| 方法 | 示例 | 结果 |
|---|---|---|
| map | [1,2,3].map((e) => e * 2) |
[2,4,6] |
| where | [1,2,3,4].where((e) => e % 2 == 0) |
[2,4] |
| fold | [1,2,3].fold(0, (a, b) => a + b) |
6 |
| reduce | [1,2,3].reduce((a, b) => a + b) |
6 |
| any | [1,2,3].any((e) => e > 2) |
true |
四、性能对比
| 方法 | 时间复杂度 | 空间复杂度 |
|---|---|---|
| map | O(n) | O(n) |
| where | O(n) | O(n) |
| fold | O(n) | O(1) |
| sort | O(n log n) | O(1) |
五、最佳实践
- ✅ 使用链式调用
- ✅ 避免不必要的toList
- ✅ 合理使用延迟计算
- ❌ 注意性能影响
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)