以下是获取前100条数据并删除它们的示例代码:
```dart
import 'package:flutter/material.dart';
class ExamplePage extends StatefulWidget {
ExamplePage({
Key key}) : super(key: key);
@override
_ExamplePageState createState() => _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
List<String> _dataList = List.generate(200, (index) => "Item $index");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Example")),
body: ListView.builder(
itemCount: _dataList.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(_dataList[index]),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
// 获取前100条数据
final first100 = _dataList.take(100).toList();
// 删除前100条数据,注意,删除的是下标0到下标99
_dataList.removeRange(0, 100);
// 可以使用 first100 进行其他操作,比如打印或传递给其他方法。
print(first100);
});
},
child: Icon(Icons.delete_outline),
),
);
}
}
In this example, we first generate a dummy data list that contains 200 string items ("item 0" to "item 199"). We ListView.builder
display the .
At the bottom of the page, we FloatingActionButton
create a button using the , and update _dataList
the list when pressed. We use take
and toList
to get the first 100 items. We then use to removeRange
remove the first 100 items. Now, only the last 100 items remain in the list.