The flutter list gets the top 100 and removes

以下是获取前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.builderdisplay the .

At the bottom of the page, we FloatingActionButtoncreate a button using the , and update _dataListthe list when pressed. We use takeand toListto get the first 100 items. We then use to removeRangeremove the first 100 items. Now, only the last 100 items remain in the list.


Guess you like

Origin blog.csdn.net/weixin_44911775/article/details/129840047
Recommended