'How to Store GetX List Data in Get Storage

Here I given Controller and widget view. Everything works fine. But When we close our app and restart the app all details got reset. so how to store this data and fetch using get storage.

import 'package:flipzon/models/product.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';

class CartController extends GetxController {
   final _products = {}.obs;

   void addProduct(Product product) {
     if (_products.containsKey(product)) {
      _products[product] += 1;
     } else {
      _products[product] = 1;
     }

     print("You have added the ${product.name} to the Cart");

     Get.snackbar(
        "Product Added", "You have added the ${product.name} to the Cart",
        snackPosition: SnackPosition.BOTTOM,
        duration: const Duration(seconds: 3));
    }

    void removeProduct(Product product) {
      if (_products.containsKey(product) && _products[product] == 1) {
      _products.removeWhere((key, value) => key == product);
      } else {
      _products[product] -= 1;
     }
  }

  get products => _products;
}

Here is my Cart View Widget

Obx(
      () => Flex(
        direction: Axis.horizontal,
        children: [
          Flexible(
              child: ListView.builder(
                  itemCount: cartController.products.length,
                  itemBuilder: (BuildContext context, int index) {
                    return ProductCard(
                        controller: cartController,
                        product:
                            cartController.products.keys.toList()[index],
                        qty: cartController.products.values
                            .toList()[index]
                            .hashCode,
                        index: index);
                  })),
        ],
      ),
    )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source