I am trying to get data from an API, which I did successfully, and show the data in a dialog box on my Flutter App.
Initially what I did was create a function void _showDialog()
right after the function where I am getting the data from the API and not only did it not work, the dialog box wasn’t showing also. So after searching around a bit I modified my code and used the following. The dialog box is now being shown, but I still cannot see the data inside the dialog.
@override
void initState() {
super.initState();
fetchDialogData();
fetchData();
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Html(data: _dnContent),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('close'),
),
],
);
},
);
});
fetchHomeBodyContent();
}
Future<void> fetchDialogData() async {
var headers = AppConstants.apiHeaders;
var request = http.MultipartRequest(
'POST',
Uri.parse(
$my_url));
request.fields.addAll(AppConstants.apiBody);
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
var responseBody = await response.stream.bytesToString();
final data = json.decode(responseBody);
final nestedDescriptionData =
data['response']['RESULT'][0]['description'];
final descriptionData = json.decode(nestedDescriptionData);
final dnContent = dnDescriptionData['dn'];
setState(() {
_dnContent = dnContent;
});
}
}
2
Answers
I created only a simple example for you complete in your project:
You can use await for fetchDialogData methods like.
However, since you haven’t provided details about the other two methods, I recommend handling them based on their behaviour and importance in your workflow.