How can we use service locator in custom flutter library or package?

195 Views Asked by At

I want to split my app into features and each feature in a package, so how can we apply dependency injection using get_it in each package individually

"each package represent a feature containing all business logic and ui for that feature"

1

There are 1 best solutions below

1
Abdelazeem Kuratem On

Create a new Interface as a contract for the methods you are going to use from this package.

Then, implement this interface in a new class and wrap the package inside this class with the implementation of the package.

Finally, use your service locator with this class in the same way that you always doing with your normal classes.

Ex.

abstract class Encryptor{

  Future<String> encrypt(String input);
  Future<String> decrypt(String input);
}


class EncryptorImpl implement Encryptor{

  Future<String> encrypt(String input){
    final result = await encrypt.encrypt("myKey", input);
    return result;
 }
  Future<String> decrypt(String input){
//same!!
.
.
  }
}