Flutter pub get taking too long

Fixing Long Flutter Pub Get Times

When running flutter pub get commands, sometimes it can take longer than expected. There are several reasons why this might happen, but here are a few potential solutions:

1. Check Your Internet Connection

Make sure that your internet connection is stable and not experiencing any slowdowns. A poor internet connection can significantly slow down the retrieval of dependencies.

2. Update Pubspec.yaml

Check your pubspec.yaml file to see if there are any unnecessary dependencies or outdated versions. Remove any unused dependencies and update the version numbers of the required ones. Also, group your dependencies logically to optimize performance. For example:


dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  http: ^0.13.3

dev_dependencies:
  flutter_test:
    sdk: flutter
  

3. Upgrade Flutter SDK

Ensure that you are using the latest version of the Flutter SDK. Flutter regularly releases updates with performance improvements and bug fixes that can speed up the flutter pub get process.

4. Use a Fast and Stable Package Mirror

The default package source might be slow for your location. Switch to a different package source by specifying a URL in your pubspec.yaml file, like below:


dependency_overrides:
  http:
    hosted:
      name: http
      url: "https://pub.flutter-io.cn"
    

Using a mirror server closer to your location can significantly improve the pub get speed.

5. Clear Pub Cache

Over time, your local package cache can become cluttered and slow down the pub get process. Run the following command to clear your pub cache:


flutter pub cache clean
  

After cleaning the cache, run flutter pub get again.

6. Parallelize the Download

By default, dependencies are downloaded sequentially, which can result in longer times. You can enable parallel downloading by running:


flutter config --enable-parallel-downloads
  

This will allow Flutter to download multiple packages simultaneously, reducing the overall download time.

7. Disable Flutter Firewall

If you are running Flutter behind a firewall, it may restrict access and slow down the pub get process. Temporarily disable the firewall and try running the command again.

By following these steps, you should be able to improve the performance of flutter pub get and reduce the time it takes to fetch dependencies for your Flutter project.

Leave a comment