Scripts in Flutter Project

Boris Dev
3 min readAug 11, 2023

--

Introduction:

A script is a type of program that consists of a sequence of instructions written in a scripting or programming language. Unlike compiled programs, which are translated into machine code that can be directly executed by the computer’s processor, scripts are interpreted or executed by another program called an interpreter.

In system administration and automation, shell scripting languages are commonly used. The most used are Bash and PowerShell. Of course scripts are used in Gaming, Server-Side Web Developing etc.

Script in Flutter Project:

In this example I will explain how I use script to help me generate code in my project.In my project, I needed a json_serializable package. In Flutter, the json_serializable package, which is available on pub.dev, is used to simplify the process of serializing and deserializing Dart objects to and from JSON (JavaScript Object Notation) format.

The json_serializable package generates the serialization and deserialization code for your Dart classes based on annotations you add to those classes. This greatly reduces the manual work required to convert between Dart objects and JSON.

I created a shell script that searches by name for all files that end with .g .dart extension and deletes and then call flutter clean && flutter pub get and generates new .g. files.

If we try to run cleanAndRebuild.sh in our terminal, we will get message command not found.

In order to solve this we have to add ./ in front of the script name:

  • If you type cleanAndRebuild.sh, the terminal will look for it in the directories specified in the PATH environment variable and might not find it.
  • If you type ./cleanAndRebuild.sh, you're telling the terminal to look for the script in the current directory, indicated by ./.

So, using ./ in front of the script name explicitly specifies that you want to run the script located in the current directory.

But when trying to run this script again , we will encounter a problem.

We get permission denied. In order to solve this we have to add some extra lines. In front of cleanAndRebuild.sh we have to add:

  • chmod: Stands for "change mode," and it's used to modify the permissions of files and directories.
  • +x: This flag adds the "execute" permission to the specified file.

Also, there are other permissions:

  • +r: This flag adds the "read" permission to the specified file.
  • +w: This flag adds the "write" permission to the specified file.

After we run chmod +x cleanAndRebuild.sh permission for execute is add and so now we just need to run again cleanAndRebuild.sh

Now the script is running successfully and it’s generating code. This example show the process of working with scripts can involve multiple steps, including setting up permissions, running the script, and benefiting from the automated tasks it performs.

This example serves as a practical illustration of the concepts you previously discussed about scripts, permissions, and the specific use case of json_serializable in a Flutter project.

--

--