Reputation: 1782
I am trying to get price instance member from another class by passing that in navigator as
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InfoPage(
image: image,
tag: tag,
name: name,
category: category,
price: price,
)));
// my price is defined inside a var products as shown below
var products = [
{
"price": " 100",
"imageLink": "images/pd.jpg",
"tag": "one",
"name": "product_1",
"category": "combo",
},
]
The instance member 'prodPrice' and 'widget.price' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
This is the error message I am getting I tried initializing my instance variable but that didnt work plz help
import import 'package:flutter/material.dart';
class InfoPage extends StatefulWidget {
final String image;
final String name;
final String price;
final String category;
final String tag;
const InfoPage(
{Key key, this.image, this.name, this.tag, this.category, this.price})
: super(key: key);
@override
_InfoPageState createState() => _InfoPageState();
}
class _InfoPageState extends State<InfoPage> {
String prodPrice;
void initState() {
super.initState();
prodPrice = int.parse(widget.price);
}
var prodPrice;
dynamic total = widget.price;
dynamic prodPrice = widget.price*2;
// the above 2 lines are giving error while using widget.price and also prodPrice.
}
Upvotes: 1
Views: 7662
Reputation: 980
You can refer to their value on initState function
class _InfoPageState extends State<InfoPage> {
var prodPrice;
dynamic total;
void initState() {
super.initState();
total = widget.price;
prodPrice = int.parse(widget.price);
}
}
Also You can check this link.
Upvotes: 3
Reputation: 2409
put them in a function and call it when ever you want
void initState() {
super.initState();
prodPrice = int.parse(widget.price);
}
dynamic total;
dynamic prodPrice;
getinit() {
total = widget.price;
prodPrice = widget.price * 2;
}
Upvotes: 1