Dart x Flutter Generate Asset.dart At VSCode 's Regex
Tags: dart, flutter, regex, vscode
Published: 2020-09-12 12:58:27

Generate asset.dart At VSCode 's Regex 
We can write following code to create Image Widget In Flutter 

```
return Image.asset("assets/images/food_purple_chocolate_dessert.jpg");
```

but, It is difficult to prove this code is corrent.
because "assets/images/food_purple_chocolate_dessert.jpg" may be not found .

So, Generate a Code, it mechanically at Regex 

like following.

```
const images_food_purple_chocolate_dessert_jpg = 'assets/images/food_purple_chocolate_dessert.jpg';
```

and use

```
return Image.asset(assets.images_food_purple_chocolate_dessert_jpg);
```

[ps]: In the future, VSCode's Flutter plug-in or will be discovered at compile time, so this is only know-how for now.

# (1) List assets image

```
:myapp kyorohiro$ find ./assets
./assets/
./assets/.DS_Store
./assets/images
./assets/images/food_pizza_slice_fast.jpg
./assets/images/food_dim_sum_squid.jpg
./assets/images/food_chili_cheese_bowls.jpg
./assets/images/flower_orange_calendula_bright_0.jpg
./assets/images/food_pasta_tomato_theme.jpg
./assets/images/food_purple_chocolate_dessert.jpg
:myapp kyorohiro$ 

```

# (2)Creaet assets.dart
```
./assets/images/food_pizza_slice_fast.jpg
./assets/images/food_dim_sum_squid.jpg
./assets/images/food_chili_cheese_bowls.jpg
./assets/images/flower_orange_calendula_bright_0.jpg
./assets/images/food_pasta_tomato_theme.jpg
./assets/images/food_purple_chocolate_dessert.jpg
```

# (3) Erase "./" 

erase "./" at vscode regex 

```
find: "\.\/" , replace: "" 
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/obzg4jvryhfyr68v65fr.png)


```
assets/images/food_pizza_slice_fast.jpg
assets/images/food_dim_sum_squid.jpg
assets/images/food_chili_cheese_bowls.jpg
assets/images/flower_orange_calendula_bright_0.jpg
assets/images/food_pasta_tomato_theme.jpg
assets/images/food_purple_chocolate_dessert.jpg
```

# (4) Convert into varable

```
find: "(.*)" , replace: "$1 = '$1'" 
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/au9lhgtnn6i7zd3b5ta0.png)

```
assets/images/food_pizza_slice_fast.jpg = 'assets/images/food_pizza_slice_fast.jpg'
assets/images/food_dim_sum_squid.jpg = 'assets/images/food_dim_sum_squid.jpg'
assets/images/food_chili_cheese_bowls.jpg = 'assets/images/food_chili_cheese_bowls.jpg'
assets/images/flower_orange_calendula_bright_0.jpg = 'assets/images/flower_orange_calendula_bright_0.jpg'
assets/images/food_pasta_tomato_theme.jpg = 'assets/images/food_pasta_tomato_theme.jpg'
assets/images/food_purple_chocolate_dessert.jpg = 'assets/images/food_purple_chocolate_dessert.jpg'
 = ''
```


# (5) "/" and "." into "_"

```
find: "(.*)/(.*) = (.*)" , replace: "$1_$2 = $3"
find: "(.*)\.(.*) = (.*)" , replace: "const $1;"  
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oxvtpmjjf5r7xjsfueu3.png)


```
assets_images_food_pizza_slice_fast_jpg = 'assets/images/food_pizza_slice_fast.jpg'
assets_images_food_dim_sum_squid_jpg = 'assets/images/food_dim_sum_squid.jpg'
assets_images_food_chili_cheese_bowls_jpg = 'assets/images/food_chili_cheese_bowls.jpg'
assets_images_flower_orange_calendula_bright_0_jpg = 'assets/images/flower_orange_calendula_bright_0.jpg'
assets_images_food_pasta_tomato_theme_jpg = 'assets/images/food_pasta_tomato_theme.jpg'
assets_images_food_purple_chocolate_dessert_jpg = 'assets/images/food_purple_chocolate_dessert.jpg'
 = ''
```



# Add "Const" and ";"


```
find: "^(.*)$" , replace: "const $1;" 
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t660byekgvylcco4txfc.png)

```
const assets_images_food_pizza_slice_fast_jpg = 'assets/images/food_pizza_slice_fast.jpg';
const assets_images_food_dim_sum_squid_jpg = 'assets/images/food_dim_sum_squid.jpg';
const assets_images_food_chili_cheese_bowls_jpg = 'assets/images/food_chili_cheese_bowls.jpg';
const assets_images_flower_orange_calendula_bright_0_jpg = 'assets/images/flower_orange_calendula_bright_0.jpg';
const assets_images_food_pasta_tomato_theme_jpg = 'assets/images/food_pasta_tomato_theme.jpg';
const assets_images_food_purple_chocolate_dessert_jpg = 'assets/images/food_purple_chocolate_dessert.jpg';


```

# PS

this know-how is from following live coding.

https://dev.to/kyorohiro/todo-dart-x-flutter-mokumoku-live-coding-12-09-2020-bbm

and code and live coding url

https://www.youtube.com/watch?v=meWE25TzQHo&list=PLWfoegDu8aWEod96swhiuzUiFLNo8Lra4&index=4&t=2540s

https://github.com/kyorohiro/mokumoku02