title: How to save list data through sharedprefrence
body: <p>I am developing an E-commerce application in flutter. I want to store cart data in SharedPreference and then display it on the cart page. Here I am storing data through SharedPreference but I get an exception . I don't know about the exception, by data is saved successfully in list.</p>
  
  <p>This is my function where I save data.</p>
  
  <pre><code>import 'package:shared_preferences/shared_preferences.dart';
  class Cart extends StatefulWidget {
  final int productId;
  Cart(this.productId);
  static const routeName = '/cart';
  @override
  _CartState createState() =&gt; _CartState();
  }
  
  
  class _CartState extends State&lt;Cart&gt; {
  List cart = [];
  List cartProduct = [];
  
  void initState() {
  getCart();
  super.initState();
  }
  Future getCart() async {
  final response = await http.get(
      'http://192.168.10.6/flutter/cat6GetbyID.php?product_id=${widget.productId}');
  if (response.statusCode == 200) {
    cart = json.decode(response.body);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setStringList("products", ['$cart']);
    setState(() {
      List&lt;String&gt; stringValue = prefs.getStringList('products') ?? [];
      cartProduct = stringValue;
    });
  }
  }
  </code></pre>
  
  <p>And in the code , in this way i am accessing it...</p>
  
  <blockquote>
    <p>Text(
                                    cartProduct[index]["product"],)</p>
  </blockquote>