Flutter:RangeError(索引):无效值:不在包含范围 0..27:28

Flutter : RangeError (index): Invalid value: Not in inclusive range 0..27: 28

大家好,我是 flutter 初学者,在练习滑动以关闭选项时,我已经完成了下面提到的代码,在删除了一些产品后,我收到了以下错误,我试图解决问题但不能,请给我宝贵的建议。

下面附上代码和错误供大家参考。

======== widgets库捕获异常================================ ======================= 以下 RangeError 被抛出构建: RangeError (index): 无效值:不在包含范围 0..27: 28

抛出异常时,这是堆栈: #0 List.[] (飞镖:core-patch/growable_array.飞镖:254:60) #1 MyHome.build。 (包:flutterswipedismiss/main.dart:34:27) #2 SliverChildBuilderDelegate.build(包:flutter/src/widgets/sliver.dart:455:22) #3 SliverMultiBoxAdaptorElement._build(包:flutter/src/widgets/sliver.dart:1201:28) #4 SliverMultiBoxAdaptorElement.createChild。 (包:flutter/src/widgets/sliver.dart:1214:55) ...

...

Main.dart

  import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHome(),
    );
  }
}

class MyHome extends StatelessWidget {
  final List<String> items =
      List<String>.generate(30, (i) => "Product ${i + 1}");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Delete panna sidela thallu"),
      ),
      body: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, int index) {
          return Dismissible(
            key: Key(items[index]),
            onDismissed: (direction) {
              items.removeAt(index);
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text("Item Dismissed."),
                ),
              );
            },
            background: Container(
              color: Colors.red,
            ),
            child: ListTile(
              title: Text("${items[index]}"),
            ),
          );
        },
      ),
    );
  }
}

我无法理解问题的原因,但我能够通过将 MyHome 小部件转换为 StatefulWidget 并在删除项目时调用 setState 来修复它。

这是新代码:

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  final List<String> items =
      List<String>.generate(30, (i) => "Product ${i + 1}");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Delete panna sidela thallu"),
      ),
      body: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, int index) {
          return Dismissible(
            key: Key(items[index]),
            onDismissed: (direction) {
              setState(() {
                items.removeAt(index);
              });
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text("Item Dismissed."),
                ),
              );
            },
            background: Container(
              color: Colors.red,
            ),
            child: ListTile(
              title: Text("${items[index]}"),
            ),
          );
        },
      ),
    );
  }
}