createEnvFamily function
Create a provider family that can return environment variable values
The provider will look for an environment file named '.env' by default,
but this can be changed by supplying a filename.
It will also check if the environment file has key-value pairs that match
the entries in requiredVersions. If the value in the file is too high,
too low, or not found, the provider will throw. This lets developers know
of breaking changes in the environment file structure.
Typical usage is to create a provider family that can then be used in other providers / consumers:
final envFamily = createEnvFamily(requiredVersions: {'VERSION': 3});
final authProvider = FutureProvider(
(ref) async => AuthManager(
clientId: await ref.watch(envFamily('CLIENT_ID').future),
),
);
Implementation
FutureProviderFamily<String, String> createEnvFamily({
String filename = '.env',
Map<String, int> requiredVersions = const {},
}) {
final envFileProvider = _envFileFamily(
_EnvFileArgs(
filename: filename,
requiredVersions: requiredVersions,
),
);
return FutureProvider.family<String, String>(
(ref, key) =>
ref.watch(envFileProvider.future).then((value) => value.env[key]!),
);
}