I have used Numbers Api for my application, but when pressed a button it show null on the screen. Also my API contains only String.
Here is my code for API:
String fact;
Future fetchData() async {
Response response;
response = await get(Uri.https('numbersapi.com', 'random/trivia'));
if (response.statusCode == 200) {
print(response.statusCode);
setState(() {
fact = response.body;
});
}
}
@override
void initState() {
fetchData();
super.initState();
}
@override
Widget build(BuildContext context) {
.....
Since Text widget not accept null values, I converted it to toString()
-
fact.toString(),
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
But when fetchData()
is passed in onPressed Property of the Elevated Button, it shows null on the screen.
How do I display a fact on screen instead of null?
Any help will be much appreciated :)