【问题标题】:How to load images from api url in flutter?如何在颤动中从 api url 加载图像?
【发布时间】:2020-05-18 04:27:37
【问题描述】:

我正在构建一个类似于 instagram 的颤振应用程序,我需要从 api 加载用户个人资料图片和帖子的图像。

我已经声明了一个函数来检索.net core中用户头像的路径:

    [HttpGet("{id}", Name = "Get")]
    public async Task<ActionResult<string>> GetAsync(string id)
    {
        ApplicationUser user = _dbContext.Users.OfType<ApplicationUser>().FirstOrDefault(x => x.Id == id);

        if (user == null)
            return BadRequest();
        return Ok(user.ProfilePicture.ToString());
    }

这是我在flutter中检索图像路径的api函数:

Future<String> getUsers() async {
final response = await http.get("http://10.0.2.2:8070/api/File/07bb2a17-7cd5-471b-973a-4b77d239b6c3");

if (response.statusCode == 200) {
  path=response.body.toString();
  x=new File(path);
 // print(response.body);
  return response.body;
} else {
  print(response.statusCode);
  // If that call was not successful, throw an error.
  throw Exception('Failed to load post');
}
}

当我尝试使用这种技术加载从 wwwroot 目录接收到的路径时:

 Container(
 height: 100.0,
 width: 100.0,
 decoration: BoxDecoration(
 shape: BoxShape.circle,
 image: new FileImage(x),
 fit: BoxFit.cover,
 ), ),

我收到以下错误:(操作系统错误:没有这样的文件或目录,errno = 2)

从 api 加载用户帖子和图像的正确方法是什么?

任何帮助将不胜感激。

【问题讨论】:

  • 改用Image.network小部件会更容易

标签: asp.net image api url flutter


【解决方案1】:

您可以使用flutter_insta 插件并像这样获取个人资料图片网址

FlutterInsta flutterInsta = new FlutterInsta("coding_boy_");
print("Profile Image : ${flutterInsta.imgurl}");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多