颤振 |如何从共享首选项中删除特定密钥

Flutter | How to remove specific key from Shared preferences

这里我想从我的购物车中删除特定产品。我的购物车是使用共享偏好数据生成的。所以基本上我想从共享首选项中删除特定索引。 希望你理解这个问题。我也在此处使用列表视图生成器来显示数据。

这是共享首选项的代码,

SharedPreferences preferences = await SharedPreferences.getInstance();
List<String> cartitems = preferences.getStringList('cartItems');

 return ListView.builder(
  physics: NeverScrollableScrollPhysics(),
  shrinkWrap: true,
  itemCount: unauthCartItems.length,
  itemBuilder: (context, index) {
    print(unauthCartItems.length);

    return FlatButton(
      onPressed: () {
        //Here i want to clear the Preferences based on index
      },
    );
  },
);

从列表中删除项目并将其保存回首选项。

     cartitems.removeAt(index);
​
     preferences.setStringList(cartitems)