从pexel api获取http请求

问题描述

我正在创建一个壁纸应用程序,我正在通过 pexel api 获取壁纸。但是当我制作一个响应变量并给它值 http.get(url);在这里,我粘贴了 api 中给出的精选壁纸的链接,但它给出了这样的错误:无法将参数类型“字符串”分配给参数类型“Uri”。谁能告诉我如何解决。和我正在关注的教程,他直接将链接粘贴为字符串,并且没有给他任何错误。 这是该代码的示例 :

import 'package:http/http.dart' as http;

getTrendingWallpaper() {
    var trendingUrl = "https://api.pexels.com/v1/curated?per_page=1";
    var response = http.get(trendingUrl); // this is giveing this error :  The argument type 'String' can't be assigned to the parameter type 'Uri'
  }

解决方法

你应该使用

var trendingUrl = Uri.parse("https://api.pexels.com/v1/curated?per_page=1");