resourceOf<T extends KareeModule> static method

String? resourceOf<T extends KareeModule>()

This function is used to get the package name by Module type.

When loading an asset, we can optionally specify the package name where we want to retrieve the asset from. We cannot get assets of the current package with non null value of this property even if it is the current package's name.

Running the root Karee project in application mode, means that the rootBundle refers to the current application, and the resources of modules should be loaded with the package name value.

To allow running a module without getting an error on loading resources from assets, we should know the mode of execution.

  • In Application Mode:

To load an image defined in the module named payment, we need to specify the name of the module.

  AssetImage('assets/images/icon_payment.png', package: 'payment');

When running application in this mode, the rootBundle refers to the current instance. So we need to set the package to allow the module to load their own asset ressources.

  • In Module Mode

To load an image defined in the current module named payment, we don't have to specify the name of the module. If we do that, it'll mean that we are looking for resources outside the current package, and we'll get an error: unable to load resource from assets.

  AssetImage('assets/images/icon_payment.png');

To avoid checking the execution mode, you just need to use PackageManager.resourceOf() to get the right package definition.

  AssetImage('assets/images/icon_payment.png', package: PackageManager.resourceOf<PaymentModule>());

Implementation

static String? resourceOf<T extends KareeModule>() =>
    KareeMaterialApp.type != KareeApplicationType.module
        ? _getModuleName<T>()
        : null;