简体   繁体   English

Flutter:如何从响应 API 显示数据 HTML 结果?

[英]Flutter : How to show data HTML result from response API?

Hello everybody I has a response data in HTML format and I want to manage the data to display in screen.大家好,我有一个 HTML 格式的响应数据,我想管理要在屏幕上显示的数据。 I use library flutter_html .我使用库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.我有 HTML,但是当我想展示它时,我得到instance of 'Future'

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.您也必须等待 getHtml(),但不能直接在 init 上执行。

void loadResponse() async {
    final response = await getHtml();
    setState(() {
        i = response;
    });
}

And then call the method on init():然后调用 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正如@Mariano所说,您需要创建新功能

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并从 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);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 如何显示 api 响应的数据 - Flutter how can i show data of api response 如何显示从 flutter 中的 API 响应中获取的 pdf? - how to show a pdf fetched from an API response in flutter? Flutter:如何将来自 api 响应的特定数据存储在列表中 - Flutter: How to store specifc data from api response in a list 如何在颤动中单击按钮时显示从 api 获取的数据? - How to show the fetched data from api on a button click in flutter? 我如何在 Flutter 的 api 响应数据上方向 listViewbuilder 显示保存的收藏夹项目? - How i can show a saved fav items to listViewbuilder above the api response data in Flutter? 如何在 flutter 中返回并显示计数结果 json 响应 api - How to return and display a count result json response api in flutter Flutter:如何从 API 获取数据并以列表形式显示 flutter 中的图像? - Flutter : How to fetch data from API and show in the list form with image in flutter? 如何在Flutter中将嵌套的JSON API响应显示为网格视图? - How to show a nested JSON api response as a grid view in flutter? 如何从 http POST 请求结果 Flutter 在列表视图构建器上显示数据 - how to show data on listview builder from http POST request Result Flutter 如何将卡片轮播的硬编码数据更改为来自 api 响应的动态数据 -Flutter - How can change my hardcoded data of a card carousel to dynamic data from api response -Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM