I have a widget in which I am passing a JSON object on my page.dart
Inside the page.dart I am (In Order) :
Defining the variable
Calling the API service
Setting the state of API response to incentivesOem
After that, I am rendering the rest of the page body and calling my widget then passing the above variables to that widget. But the service response is not captured when it returns to page.dart and incentivesOem is equal to null
First - assign variables (✅)
List<IncentiveOem> incentivesOem;
IncentiveServiceOem incentiveServiceOem = new IncentiveServiceOem();
Second - call service (Here in the service the api returns a JSON body ✅)
incentiveServiceOem.getIncentivesOem(userRequestModel).then((incentives) {
setState(() {
incentivesOem = incentives;
isLoaded = true;
});
});
Third - Use the incentivesOem variable in the list
I call the Widget IncentivesListOem(**incentivesOem**);
But when the page loads my **incentivesOem** = **null**
IncentivesListOem(incentivesOem: incentivesOem)
The problem was in the end how I was accessing the data through the widget.
class IncentivesListOem extends StatefulWidget {
@override
State<IncentivesListOem> createState() => _IncentivesListOemState();
// Over here I needed to add "this.incentivesOem"
IncentivesListOem({Key key, @required this.incentivesOem}) : super(key: key);
final IncentiveOem incentivesOem;
}
CodePudding user response:
It's because you've defined the variable incentivesOem
but not initialized, initialize the variable as below,
List<IncentiveOem> incentivesOem = [];
CodePudding user response:
So the problem was in my widget.
class IncentivesListOem extends StatefulWidget {
@override
State<IncentivesListOem> createState() => _IncentivesListOemState();
// Over here I needed to add "this.incentivesOem"
IncentivesListOem({Key key, @required this.incentivesOem}) : super(key: key);
final IncentiveOem incentivesOem;
}
Then I was able to access the variable using widget before my variable,
widget.incentivesOem
Then I was able to output the variable like so :
title: Text(
"${incentivesOem.name}",
),