从http请求加载图像

问题描述 投票:0回答:1

我正在开发一种必须获得不同价值的个人资料卡。我得到了所有的价值,但我也想加载网络图像。我正在使用文件制作服务器,我注意到我需要coockies来加载它。当我发出请求时,将图片网址粘贴到我的浏览器中,它就会加载。但无论何时我将它加载到我的应用程序中,我都会获得带有图像的401 statusCode。

我已经尝试了一个有效的网络图像,我已经知道有关coockies的东西,但我不确定我是否需要使用它们以及如何使用它们。我也觉得很奇怪,每当我在浏览器中加载图像时,它都可以工作但不能在我的应用程序上运行。

未来makeRequest()async {var url4 =“https://fms.xxxxxx.nl/fmi/data/v1/databases/Roscom管理系统/布局/员工pa api / _find”; var body = json.encode({“query”:[{“Email address(1)”:“[email protected]”}],});

Map<String, String> headers = {
  'Content-type': 'application/json',
  'Accept': 'application/json',
  "Authorization":
      'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
};

var response = await http.post(url4, body: body, headers: headers);
setState(() {
  var responseJson = json.decode(response.body);
  data = responseJson['response']['data'][0];
  profielfoto = data['fieldData']['iMedewerker Foto'];

  print(profielfoto);
});

Value i get in the terminal

我希望我可以只使用var $ profielfoto在网络图像中加载图像。我不知道该怎么做coockies或者可能有一个更简单的方法来做它。我希望有人可以帮助我,如果我需要提供有关服务器或其他任何信息的更多信息,请告诉我。 ;)

http dart flutter filemaker
1个回答
0
投票

一些东西。请不要在setState中放置任何类型的重处理

https://docs.flutter.io/flutter/widgets/State/setState.html

通常,建议仅使用setState方法将实际更改包装到状态,而不是任何可能与更改关联的计算。例如,这里构建函数使用的值递增,然后将更改写入磁盘,但只有增量包含在setState中:

setState告诉窗口小部件何时需要重绘

https://flutter.dev/docs/cookbook/images/network-image

String _profilePhoto = "";
//Change await into an async operation
http.post(url4, body: body, headers: headers).then( (response) {
   var responseJson = json.decode(response.body);


   print(data['fieldData']['iMedewerker Foto']);
   setState(() {

      _data = responseJson['response']['data'][0];
     _profilePhoto = data['fieldData']['iMedewerker Foto'];
   });
})

Widget build(BuildContext context){
      //Check if string is empty
     if ( _profilePhoto == "" ){
          return CircularProgressIndicator();
     }else {
           return Image.network( _profilePhoto );
     }
}

https://flutter.dev/docs/cookbook/images/network-image

https://pub.dartlang.org/packages/cached_network_image

您有两种方法可以从网络中获取图像。我相信我提出了一种方式。

© www.soinside.com 2019 - 2024. All rights reserved.