You'll need a StatefulWidget
:
class YellowBird extends StatefulWidget {
const YellowBird({ Key? key }) : super(key: key);
@override
_YellowBirdState createState() => _YellowBirdState();
}
class _YellowBirdState extends State<YellowBird> {
// create variable to hold your data:
dynamic data;
// change dynamic to the type of the data you have
// note: it will be null by default so might have to give
// it an initial value.
Future<void> getData() async {
final response = await Provider.of<PostApiService>(context, listen:
false).getData(1.toString() + '/service_contracts');
final _getData = GetModel.fromJson(response.body);
print(_getData.company_name);
// now set the state
// this will rebuild the ui with the latest
// value of the data variable
setState(() => data = _getData);
}
@override
Widget build(BuildContext context) {
return Scaffold(body: Column(
children: <Widget>[
Text('Data is: $data'),
TextButton(
child: Text('Get data'),
onPressed: getData,
),
], ), );
}
}