How to clear Flutter’s Build cache?

In a typical development workflow with Flutter, clearing the build cache can be essential to resolve issues related to asset files not being updated, or even just to ensure that your code changes are reflected in the app. Let’s take a look at how you can do this.

Clearing Build Cache via Command Line

The most direct way is by running the command flutter clean. This command will delete all the files generated by Flutter, including the cache, in your project directory. If you’re using a version control system like Git, this might trigger a commit with changes from your IDE.

flutter clean

Clearing Build Cache via Android Studio (or IntelliJ IDEA)

This method is similar to the one mentioned above but instead of running a command in your terminal, you’ll be doing it directly within your IDE. Open your project in Android Studio and follow these steps:

  1. Go to: Tools -> Flutter -> Clean
  2. Click on Clean. This will remove the build cache.

This should resolve any issues you were experiencing. Note that it doesn’t delete your code files or assets but rather clears out the compiled code and other intermediate files that Flutter uses to run the app.

Alternative Method: Running Your App from Command Line

In case you’re still encountering problems with the build cache, trying running your app using the command line could help. Instead of running flutter run, which typically launches the app as it’s configured in your IDE, try running your app directly from the command line using:

flutter run

This approach can sometimes bypass issues related to asset files not being updated or the build cache itself. However, remember that hot reloads and full-reloads are different. Hot reloads only update code changes without requiring a rebuild of your app, which is faster but might not always be what you need if assets have changed.

To achieve this with hot reload, press r on your keyboard. To force a full rebuild (which updates the cache and may include asset files), press R.

flutter run
r for hot reload
R for full rebuild

Conclusion

In conclusion, clearing Flutter’s build cache is not just about deleting unnecessary files; it can also be a way to resolve issues that seem unrelated. Whether you choose to do this via the command line or directly within your IDE, there are solutions available. Remembering how to run your app from the terminal and making use of hot reloads (or full rebuilds) might help you avoid these problems in the first place.