DEV Community

Cover image for A better way of using flutter packages
Yunwen Eric
Yunwen Eric

Posted on

A better way of using flutter packages

Context

I have been using package_a (ex: fluttertoast package) in my flutter project and it worked well. However, for some reasons, I have decided to change to a similar but better package.

Problem:

As soon as I change this package in my pubspec.yaml file, my IDE throws missing imports errors all over my codebase

  1. Wrong way

Wrong method of implementation

  1. Better Approach Create a separate class with a static method to display your toast

Better approach

Call the static Method

In your app’s UI, invoke the static method to display your toast

Calling static methods

What did we fix?

The alert_service has the class for showing the alert. Rather than changing the implementation in all of our app’s UI as in the former, in the later, we just need to change the package in the alert_service where it is used

Final Thoughts

Depending on other flutter packages is very important in flutter projects, however, it is important that even though we depend on these packages, we write modular and reusable code that is not too tightly coupled to dependencies.

Let's connect

Follow on X
Follow on LinkedIn

Top comments (0)