在 Flutter 中使用 Wikipedia API 获取图片不起作用

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

这是我的数据:

  Activity(
    name: 'Times Square',
    continentType: ContinentType.northAmerica,
    description:
        'A vibrant intersection and tourist destination in New York City.',
    location: const PlaceLocation(
        latitude: 40.758896,
        longitude: -73.985130,
        street: 'Broadway',
        city: 'New York City',
        country: 'USA'),
  ),

我想用这个来获取图像:

Future<String> fetchImage(String activityName) async {
    final formattedName = activityName.replaceAll(' ', '_');
    final formattedLink =
        'http://en.wikipedia.org/w/api.php?action=query&titles=$formattedName&prop=pageimages&format=json&pithumbsize=1000';
    final response = await http.get(Uri.parse(formattedLink));

    final Map<String, dynamic> data =
        json.decode(response.body) as Map<String, dynamic>;
    final pages = data['query']['pages'];
    final pageId = pages.keys.first;
    final imageUrl = pages[pageId]['thumbnail']['source'] as String;
    return imageUrl;
  }

我什至没有找到地图代码。

有什么帮助吗?

我应该得到这样的回复:

{
    "batchcomplete": "",
    "query": {
        "normalized": [
            {
                "from": "Times_Square",
                "to": "Times Square"
            }
        ],
        "pages": {
            "31389": {
                "pageid": 31389,
                "ns": 0,
                "title": "Times Square",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/New_york_times_square-terabass.jpg/1000px-New_york_times_square-terabass.jpg",
                    "width": 1000,
                    "height": 666
                },
                "pageimage": "New_york_times_square-terabass.jpg"
            }
        }
    }
}
json flutter dart wikipedia wikipedia-api
1个回答
0
投票

它与此命令一起使用:

flutter run -d chrome --web-browser-flag“--disable-web-security”

还有更好的解决方案吗?

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