如何使用Bing网页搜索Apiv7在Flutter应用程序中获取照片?

我从官方站点获得了API和端点,但是无法在项目生成器子级中获取JSON文件来调用它。

子示例:new Image.network('${data['value']['webSearchUrl']}'
我不知道要在此子项中放置什么以及将API密钥放在何处...

class _PageOneState extends State<PageOne> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new FutureBuilder(
        future: getPics(),
          builder: (context, snapShot){
          Map data = snapShot.data;
          if(snapShot.hasError){
            print(snapShot.error);
            return Text('Failed to get data from server',
              style: TextStyle(color: Colors.red,
              fontSize: 22.0),);
          }else if(snapShot.hasData){
            return new Center(
              child: new ListView.builder(
                itemCount: data.length,
                  itemBuilder: (context, index){
                  return new Column(
                    children: <Widget>[

                      new Container(
                        child: new InkWell(
                          onTap: (){},
                          child: new Image.network(
                            '${data['value']['webSearchUrl']}'
                          ),
                        ),
                      ),
                      new Padding(padding: const EdgeInsets.all(5.0)),
                    ],
                  );
                  }),
            );

          }
          else if(!snapShot.hasData){
            return new Center(child: CircularProgressIndicator(),);
          }
          }
      ),

    );
and below the code -


Future<Map> getPics() async{
  String url =
  'https://api.cognitive.microsoft.com/bing/v7.0/images';
  http.Response response = await http.get(url);
  return json.decode(response.body);
}

最佳答案

webSearchUrl指向包含搜索结果的网页,这就是为什么它在Image.network()内部不起作用的原因。尝试将其更改为contentUrl以获得完整图像ur;从第三方网站或thumbnailUrl中获取必应托管的小缩略图网址。

关于image - 如何在我的Flutter应用程序中从Bing Web Search APIv7获取图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54747760/

10-11 22:04
查看更多