4

I'm using the image picker in flutter and trying to get the image. Before I select the image it appear like this:

Before Upload Image

enter image description here

After Upload Image

enter image description here

here is the code:

backgroundImage: _pickedImage != null
          ? Image.file(_pickedImage!) as ImageProvider
          : const AssetImage('assets/images/profile-icon.png'),

How should solve this error?

Updated Error

enter image description here

https://codeshare.io/oQpBvX

kiku
  • 147
  • 1
  • 10

5 Answers5

2

Use FileImage instead like so:

backgroundImage: _pickedImage != null
          ? FileImage(_pickedImage!)
          : const AssetImage('assets/images/profile-icon.png'),
Josteve
  • 11,459
  • 1
  • 23
  • 35
  • It can't work too. I tried before. I updated the error on question. – kiku Jan 31 '22 at 17:39
  • 1
    Hey @kiku, it seems as if you have used `const` in your `DecorationImage` or `BoxDecoration` or `Container`...Please remove it, it should work. – Josteve Jan 31 '22 at 17:44
  • Hey @Josteve, I sure that I not add any const there, i share the file in the link , u can help me take a look on there – kiku Jan 31 '22 at 17:52
1

This is what you are looking for:

backgroundImage: _pickedImage != null
          ? FileImage(_pickedImage!)
          : AssetImage('assets/images/profile-icon.png'),
Jahn E.
  • 1,228
  • 3
  • 6
  • 21
1

Do this instead, render your picked image with an image.file() widget then toggle the container with the image.file()

child: image == null
        ? Container(
            width: 400.w,
            height: 300.h,
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: ExactAssetImage(imageUrl), fit: BoxFit.cover)),
          )
        : Image.file(
            image!,
            width: 160.w,
            height: 160.h,
            fit: BoxFit.cover,
          )
Porayman
  • 26
  • 1
0
///I got the same issue while I am checking image is null or not I am using a container to show the image Hope this will help you,Thanks
    //////image inside a container 
        
         Container(
                                                height: 120,
                                                width: 120,
                                                decoration: BoxDecoration(
                                                  color: Colors.white,
                                                  shape: BoxShape.circle,
                                                  image:DecorationImage(
                                                      fit: BoxFit.cover,
                                                      image: provider.currentLoggedInUser!.profilePicture!= null?
                                                      NetworkImage(provider.currentLoggedInUser!.profilePicture.toString())
                                                          :AssetImage('assets/managerPicture.jpeg') as ImageProvider),
                                                  border: Border.all(
                                                      color: AppColors.white, width: 2.0),
                                                ),
                                              ),
                                            ],
                                          ),
    

////you can also use the CachedNetworkImage plugin

ChachedNetworkImage

     CachedNetworkImage(
          maxHeightDiskCache: 100,
         imageUrl: provider.currentLoggedInUser!.profilePicture.toString(),
 imageBuilder: (context, imageProvider) => Container(
       height: 120,
       width: 120,
       decoration: BoxDecoration(
       shape: BoxShape.circle,
       image: DecorationImage(
       image: imageProvider, fit: BoxFit.cover),
       border: Border.all(
       color: AppColors.white, width: 2.0),
    
              ),
                 ),
     placeholder: (context, url) =>
    const CircularProgressIndicator(),
    errorWidget: (context, url, error) => Container(
     height: 120,
      width: 120,
      decoration: BoxDecoration(
      color: Colors.white,
      shape: BoxShape.circle,
      image:DecorationImage(
      fit: BoxFit.cover,
      image: AssetImage('assets/managerPicture.jpeg')),
      border: Border.all(
      color: AppColors.white, width: 2.0),
              ),
            ),
      fadeOutDuration: const Duration(seconds: 1),
      fadeInDuration: const Duration(seconds: 3),
          ),


  
Vishal_VE
  • 1,852
  • 1
  • 6
  • 9
0
backgroundImage: _pickedImage != null
      ? FileImage(_pickedImage!) 
      : const AssetImage('assets/images/profile-icon.png') as ImageProvider,
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Oct 04 '22 at 00:01