0

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

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

未来 makeRequest() async { var url4 = " https://fms.xxxxxx.nl/fmi/data/v1/databases/Roscom Management System/layouts/medewerker pa api/_find"; var body = json.encode({ "query": [ {"Emailadres(1)": "xxxx@xxx.nl"} ], });

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);
});

我在终端获得的价值

我希望我可以仅使用 var $profielfoto 将图像加载到网络图像中。我不知道如何处理这些 coockies,或者也许有更简单的方法可以做到这一点。我希望有人可以帮助我,如果我需要提供有关服务器或其他任何信息的更多信息,请告诉我。;)

4

2 回答 2

0

授权令牌必须是问题。当用户登录时,安全服务器将返回此令牌。然后所有随后的请求都必须在“授权标头”中包含此令牌。如果它不存在或不正确,则服务器返回 401

于 2019-10-03T01:58:40.760 回答
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

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

于 2019-04-02T13:32:47.113 回答