2

Guess I have a flutter project with assets in the following folder structure

assets -> 
images ->
 img1.png
 img2.png ...

So assume I am using this img2.png in multiple files in my flutter project, and after some time I change the assets folder structure as follows since my assets folder gets overloaded with images. So the new asset folder structure would be like,

assets ->
 images ->
  login ->
   img1.png
  signin ->
   img2.png
   ...

So in the above scenario I have changed the image folder structure based on a feature. If so, since I have used the img2.png image file in multiple files, I have to change it in all the files which is really a burden. Is there any other alternative or possibly an easier way to reflect the image path changes based on the folder structure we have redefined in the assets folder?

Thanks in advance.

codmytsvg
  • 35
  • 4
  • Usually this is changed with a global replace on the project. I don't think there's a good utility in the framework to change it automatically (refactoring) as of now. The way to change it depends on your IDE. Usually it is good practice to have the path in only 1 place to avoid those problems. – Cavitedev May 23 '22 at 18:39

1 Answers1

0

You could create a static class for your app assets, so that you'll only have to change the file path in the class

For Example

class AppAssets{
static const loginImg1 = "assets/images/login/img1.png";
static const loginImg2 = "assets/images/login/img2.png";
} 

// Then access it in your widgets like

Image.asset(AppAssets.loginImg1)

PIE
  • 46
  • 5
  • Hey @PIE, is it the best solution we can use? – codmytsvg May 23 '22 at 11:26
  • IMO, this is what i do and I have seen other people do this as well. There're probably better ways but this is what works for me. So yeah, it's a promising one, except someone else brings up a better solution. Yes – PIE May 23 '22 at 11:43