如何通过颤振从 api 中获取价值
Posted
技术标签:
【中文标题】如何通过颤振从 api 中获取价值【英文标题】:how to get value from api with flutter 【发布时间】:2021-07-24 22:08:09 【问题描述】:我有假的 Api
https://fakemyapi.com/api/fake?id=220e0e14-8c78-45e9-9ef0-6ca516fde5be
我想打印一些数据,我使用了这段代码
var headers =
'Cookie': '__cfduid=d99061ead63f349023a08a33868eb7ef81619925287'
;
var request = http.Request('GET', Uri.parse('https://fakemyapi.com/api/fake?id=220e0e14-8c78-45e9-9ef0-6ca516fde5be'));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200)
print(await response.stream.bytesToString());
else
print(response.reasonPhrase);
我得到了这个回复。 I/flutter (17790): "first_ame":"Miguel","last_name":"Fay","photo":"https://s3.amazonaws.com/uifaces/faces/twitter/dhooyenga/128.jpg ","email":"Keven.Cole@gmail.com","title":"区域功能开发人员","job_type":"主管","电话":["567.700.9452","1-701- 720-0774 x9918","1-716-687-6317 x670"],"address":"zip_code":"20909","street":"Myrtis Pines","city":"West Mekhifort","国家":"希腊","朋友":["first_name":"Carlie","last_name":"Kilback","email":"Erich_Emmerich90@gmail.com","first_name":"Clarabelle ","last_name":"Runolfsson","email":"Elise_Schroeder@gmail.com"]
那么如何打印名字和邮政编码?
【问题讨论】:
【参考方案1】:我终于通过一次又一次的尝试解决了这个问题,
var headers =
'Cookie': '__cfduid=d99061ead63f349023a08a33868eb7ef81619925287'
;
var request = http.Request(
'GET',
Uri.parse(
'https://fakemyapi.com/api/fake?id=220e0e14-8c78-45e9-9ef0-6ca516fde5be'));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200)
var item =
JsonDecoder().convert("$await response.stream.bytesToString()");
print("item $item");
print("first_ame $item['first_ame']");
return item;
else
print(response.reasonPhrase);
【讨论】:
以上是关于如何通过颤振从 api 中获取价值的主要内容,如果未能解决你的问题,请参考以下文章