1

I have tried in many ways to pass the variable points but I can not use it in another class below I leave the basic code.the variable in question is points which is located in the pointspage and I would like to use it in cartpage

CartPage

class CartPage extends StatefulWidget {
 

  _CartPagestate createState() => _CartPagestate();
}


class _CartPagestate extends State<CartPage> {
  @override
  Widget build(BuildContext context) {//some code...}

PointsPage

class points extends StatefulWidget {
  _pointsState createState() => _pointsState();
}

class _pointsState extends State<cubox> {
  int points = 0;

  @override
  void initState() {
    super.initState();
    _loadPoints();
  }

  _loadPunti() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      punti = (prefs.getInt('points') ?? 0);
    });
  }

  _savePunti() async {
    
  SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt('points', points);
  }

  @override
  Widget build(BuildContext context) { // some code...}

the variable in question is points which is located in the pointspage and I would like to use it in cartpage. the cartpage also has a cardmodel I would like to use the points variable to make "purchases" but I can't.

Cartmodel

class CartModel extends ChangeNotifier {

  // list of items on sale
  final List _shopItems = const [
    // [ itemName, itemPrice, imagePath, color ]
    ["Avocado", "4", "assets/images/avocado.png", Colors.green],
    ["Banana", "2", "assets/images/banana.png", Colors.yellow],
    ["chicken", "12", "assets/images/chicken.png", Colors.brown],
    ["water", "1", "assets/images/water.png", Colors.blue],
    ["apple", "3", "assets/images/mela.png", Colors.red],
    ["chocolate", "3", "assets/images/broccoli.png", Colors.brown],
  ];

  final List _other = const [
    ["Avocado", "14000", "assets/images/avocado.png", Colors.orange],
    ["Banana", "2", "assets/images/banana.png", Colors.blueGrey],
    ["chicken", "12", "assets/images/chicken.png", Colors.red],
    ["water", "1", "assets/images/water.png", Colors.blue],
  ];

  // list of cart items
  final List _cartItems = [];

  get other => _other;

  get cartItems => _cartItems;

  get shopItems => _shopItems;

  // add item to cart
  void addItemToCart(int index) {
    _cartItems.add(_shopItems[index]);
    notifyListeners();
  }

  // remove item from cart
  void removeItemFromCart(int index) {
    _cartItems.removeAt(index);
    notifyListeners();
  }

  // calculate total price
  String calculateTotal() {
    double totalPrice = 0;
    for (int i = 0; i < cartItems.length; i++) {
      totalPrice += double.parse(cartItems[i][1].toString());
    }
    return totalPrice.toStringAsFixed(2);
  }
}
sams
  • 43
  • 6
  • Do you want to access `points ` when `navigate ` or from `any place`? – Sukaina Ahmed Mar 13 '23 at 13:05
  • I would like the points accumulated in the pointspage to be used as "credit" within the cartpage, i.e. when the user clicks a button like "buy" the points necessary for the purchase are subtracted, I've been trying for months – sams Mar 13 '23 at 13:09
  • As in your code using `sharedPreference` is a good idea , but what is the problem with get points from `sharedPreference`? – Sukaina Ahmed Mar 13 '23 at 13:17
  • when I try to import the variable it doesn't let me use it it says it doesn't exist for pointspage but in reality it exists and it is used inside the pointspage, it doesn't let me use it to create a points subtraction method – sams Mar 13 '23 at 13:20
  • can you share the error. – Jay Mar 13 '23 at 13:32
  • "Il getter 'points' non è definito per il tipo 'pointspage'. " – sams Mar 13 '23 at 13:34
  • when i try to use it outside of pointspage – sams Mar 13 '23 at 13:34
  • You can use it by `prefs.getInt('points')` in any page ,points in pointspage is not `a global variable`. – Sukaina Ahmed Mar 13 '23 at 13:37
  • where should i add prefs.getInt('points') – sams Mar 13 '23 at 13:58

1 Answers1

0

If you want to access a variable from another class ,make the variable static and use it by calling ClassName.VariableName in the other class.

//global value 
int globalPoints = 23;


class MyWidget extends StatefulWidget {
  MyWidget({super.key});
  // variable inside first class 
  static int counter = 1;

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

class MyWidget2 extends StatefulWidget {
  const MyWidget2({super.key});

  @override
  State<MyWidget2> createState() => _MyWidget2State();
}

class _MyWidget2State extends State<MyWidget2> {
  //accessing it inside the other class 
  int counter2 = MyWidget.counter;
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}


Saad
  • 539
  • 2
  • 19