Hello everybody I has a response data in HTML format and I want to manage the data to display in screen. I use library flutter_html
.
This is my code
Future<String> getHtml() async {
var headers = {
'Cookie': 'ci_session=hgiugh'
};
var request = http.MultipartRequest('POST', Uri.parse('http://xxx/get'));
request.fields.addAll({
'': '',
});
request.headers.addAll(headers);
final http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print("HTML_RESULT"+await response.stream.bytesToString());
}
else {
print(response.reasonPhrase);
}
return await response.stream.bytesToString();}
I've the HTML but I got instance of 'Future'
when i want to show it.
Code for show the data
var i;
@override
void initState() {
// TODO: implement initState
super.initState();
i = getHtml();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: Html(
data: i.toString(),
),
),
),
);
}
Please give me a help guys, any reference is important to me, thank you.
Try changing the
final http.StreamedResponse response = await request.send();
for:
http.Response response = await http.Response.fromStream(await request.send());
You have to await the getHtml() too, but you can't do it directly on init.
void loadResponse() async {
final response = await getHtml();
setState(() {
i = response;
});
}
And then call the method on init():
@override
void initState() {
// TODO: implement initState
super.initState();
loadResponse();
}
Extract this line into seperated code
print("HTML_RESULT"+await response.stream.bytesToString());
like
var responseString = await response.stream.bytesToString();
print("HTML_RESULT $responseString");
EDIT:
As of @Mariano said, you need to create new function
void load() async {
... do your await here
... and after you got the value call
setState(() {
... set your value here
});
}
and call that function from initState
@override
void initState() {
// TODO: implement initState
super.initState();
load();
}
if (responseString == null ||
responseString.contains("A PHP Error was encountered") ||
responseString.contains("<div") ||
responseString.contains("</html")) {
Fluttertoast.showToast(
msg: "Server Error",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.