Skip to content

Commit 88427cd

Browse files
committed
feat(service): add package info service for app version retrieval
- Introduce PackageInfoService abstract class for retrieving app version - Implement PackageInfoServiceImpl using package_info_plus package - Add logging for successful fetch and potential failures - Ensure null safety and platform compatibility
1 parent 59aca7a commit 88427cd

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:logging/logging.dart';
2+
import 'package:package_info_plus/package_info_plus.dart';
3+
4+
/// {@template package_info_service}
5+
/// An abstract service for retrieving application package information.
6+
///
7+
/// This interface allows for mocking and provides a clean way to access
8+
/// platform-specific app details like version, build number, etc.
9+
/// {@endtemplate}
10+
abstract class PackageInfoService {
11+
/// {@macro package_info_service}
12+
const PackageInfoService();
13+
14+
/// Retrieves the application's version string (e.g., "1.0.0").
15+
///
16+
/// Returns `null` if the version cannot be determined (e.g., on unsupported
17+
/// platforms or during an error).
18+
Future<String?> getAppVersion();
19+
}
20+
21+
/// {@template package_info_service_impl}
22+
/// A concrete implementation of [PackageInfoService] using `package_info_plus`.
23+
/// {@endtemplate}
24+
class PackageInfoServiceImpl implements PackageInfoService {
25+
/// {@macro package_info_service_impl}
26+
PackageInfoServiceImpl({Logger? logger})
27+
: _logger = logger ?? Logger('PackageInfoServiceImpl');
28+
29+
final Logger _logger;
30+
31+
@override
32+
Future<String?> getAppVersion() async {
33+
try {
34+
final packageInfo = await PackageInfo.fromPlatform();
35+
_logger.info(
36+
'Successfully fetched package info. Version: ${packageInfo.version}',
37+
);
38+
return packageInfo.version;
39+
} catch (e, s) {
40+
_logger.warning(
41+
'Failed to get app version from platform. '
42+
'This might be expected on some platforms (e.g., web in certain contexts).',
43+
e,
44+
s,
45+
);
46+
return null;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)