flutterflutter-pubspec

Is there a way to change the name in the pubspec yaml file and automatically update all file import statements? (Flutter)


A small formatting question using Flutter with Android Studio:

I want to change the name in my pubspec.yaml file (I have changed my app's name since starting).

The minor (but very time intensive) issue is if I change the name I then need to update all my import statements in my files which turns into quite a large task (I regret not using relative import statements).

So I am wondering if there an easier way to do this?

enter image description here


Solution

  • I use Find and Replace

    1. Change the app name in your pubspec.yaml file:

    2. Press Ctrl + Shift + R (Windows/Linux) or Cmd + Shift + R (Mac) to open the "Find and Replace in Path" dialog.

    3. In the "Find" field, enter the old app name (e.g., import 'package:old_app_name).

    4. In the "Replace with" field, enter the new app name (e.g., import 'package:new_app_name).

    5. Make sure the "File mask" field is set to *.dart to search only in Dart files.

    6. Choose the "Directory" scope and ensure that the root directory of your project is selected.

    7. Click on the "Find" button to show the search results.

    8. Review the changes, and if everything looks good, click on the Replace All.