How to load images from api url in flutter?

842 views
Skip to first unread message

Amena Deeb

unread,
Feb 2, 2020, 6:06:01 AM2/2/20
to Flutter Development (flutter-dev)
Dear flutter developers,

Im building a flutter app similar to instagram, i need to load the images of the user's profile picture and posts from the api where my backend is based on .net core c#.

I have already declared a function to retrieve the path of the user profile picture in .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());
        }


and this is my api function in flutter to retrieve the path of the image:

    Future<String> getUsers() async {

    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');
    }
    }

When im trying to load the path received from the wwwroot directory using this technique: (path is like : D:\Xperience..)
  
     Container(
     height: 100.0,
     width: 100.0,
     decoration: BoxDecoration(
     shape: BoxShape.circle,
     image: new FileImage(x),
     fit: BoxFit.cover,
     ), ),

im getting the following error :(OS Error: No such file or directory, errno = 2)


What's the correct way to load user posts and images from api? 

Ive been told to use Image.network but nothing is being displayed when i provide the response path in it.

Any help would be appreciated.

Shafqat Nadeem

unread,
Feb 2, 2020, 1:52:25 PM2/2/20
to Flutter Development (flutter-dev)
ClipOval(
                          child: Image.network(urltoimg),
                        ),

the code above might help you. just try it.

Shafqat Nadeem

unread,
Feb 2, 2020, 2:00:42 PM2/2/20
to Flutter Development (flutter-dev)
just try to make it simple,

declare a future methord to load the api url , 

Map data;
String imgurl;

Future image_download_method() async {

http.Response image_url_response = await http.get("http://url-to-image");

data = json.decode(image_url_response.body);

setState(){

imgurl = data;

}






@override
init () {
super.initiate();
//todo something here
image_download_method() ;

}

bipin tripathi

unread,
Feb 10, 2020, 5:36:19 AM2/10/20
to Flutter Development (flutter-dev)
use cached_network_image plugin and widget below for profile picture

ClipOval(
child: CachedNetworkImage(
height: 30,
width: 30,
imageUrl:
imageurl,
fadeInCurve: Curves.
fastOutSlowIn,
placeholder: (context, url) =>
SizedBox(
height:
30,
width:
30,
child:
CircularProgressIndicator(
strokeWidth:
1,
),
),
errorWidget: (context, url, error) =>
Container(
color: Colors.
black87,
width:
30,
height:
30,
child:
Icon(
MdiIcons.
face,
color: Colors.
white30,
),
),
),
)


and your async function goes somewhat like this

void fetchImage()  async{
 var _imageurl = awai t  //your network call
 setState((){
 imageurl = _imageurl
 });
Reply all
Reply to author
Forward
0 new messages