【问题标题】:RangeError : RangeError(index) : invalid value : only valid is 0: 3RangeError : RangeError(index) : 无效值 : 仅有效为 0: 3
【发布时间】:2020-10-31 13:32:32
【问题描述】:

我在这个屏幕篮中展示了我篮子中可用的产品,效果很好。我使用可驳回来删除我的购物篮中的一个项目。而且我经常时不时报错: RangeError: RangeError (index): invalid value: only valid is 0: 3. 请问怎么办?

这是我的购物篮的界面。篮子屏幕。颤振清洁是最好的解决方案吗?我害怕尝试。颤振清洁究竟是做什么的?我需要一个解决方案

class PanierScreen extends StatefulWidget {


@override
  _PanierScreenState createState() => _PanierScreenState();
}

class _PanierScreenState extends State<PanierScreen> with AutomaticKeepAliveClientMixin {


  @override
  Widget build(BuildContext context) {
    List<Produit> produits = Provider.of<Panier>(context).produits ;
    Panier _panier = Provider.of<Panier>(context, listen : false);

    super.build(context);
    return  MaterialApp(
      debugShowCheckedModeBanner: false,
        title: "Panier",
        home: Scaffold(
          appBar: AppBar(
            title: Text("Panier"),
            centerTitle: true,
          ),
          body: Column(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Expanded(child: ListView.separated(
                
          itemCount: produits.length,
                  itemBuilder: (BuildContext context, int index) {
                return  Dismissible(
                  key: UniqueKey(),
                  direction: DismissDirection.endToStart,
                  background: new  Container(
                child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Center(
                      child: Icon(
                        Icons.delete, color: Colors.white,),
                    ),
                    Spacer(),
                    Text("Supprimer", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),)
                  ],
                ),
                color: Colors.red,
                ),
                  onDismissed: (direction) {

                  //  _panier.retirerProduit(produits[index]);
                    produits.removeAt(index);
                    print(produits.length);
                    Scaffold.of(context).showSnackBar(new SnackBar(
                        content: new Text("Produit supprimé du panier"),
                    duration: Duration(seconds: 3),)); },


                  child: Card (
                    child: ListTile(
                      leading: CircleAvatar(
                        child: FittedBox(
                          child: Text("${produits[index].prixvente} FCFA"),
                        ),
                      ),
                      title: Text("${produits[index].designation}".toUpperCase(), style: TextStyle(fontWeight: FontWeight.bold),),
                      subtitle: Text("Total : ${produits[index].quantite_vendue * produits[index].prixvente} FCFA", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red[300]), ),
                      trailing: Text("${produits[index].quantite_vendue.toString()} x", style: TextStyle(fontWeight: FontWeight.bold),),
                    ),
                  ),
                );
              },
                  separatorBuilder: (BuildContext context, int index) => Divider(),
                  ))
            ],
          ),
        ),
    );
  }
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    flutter clean 将重建 /build 文件夹(Flutter 的构建缓存)。这是一个运行的安全命令,但我认为它与您的问题无关。

    似乎您正在从您的List produits 中删除某个索引处的项目,他们试图在代码中稍后访问它。

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 2021-06-16
      • 2020-06-24
      • 2022-08-17
      • 2022-11-23
      • 2021-01-01
      • 2022-07-18
      • 2020-10-07
      • 2020-04-19
      相关资源
      最近更新 更多