From 12f5fd2c0a9703d4ea4e142b3e037ace3f06b2f6 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Fri, 29 Sep 2023 21:59:11 +0200 Subject: [PATCH] refactor(neon): throw an error if neonUserAgent hasn't been set up Signed-off-by: Nikolas Rimikis --- packages/neon/neon/lib/src/utils/user_agent.dart | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/neon/neon/lib/src/utils/user_agent.dart b/packages/neon/neon/lib/src/utils/user_agent.dart index f8f924ae..3685dcc1 100644 --- a/packages/neon/neon/lib/src/utils/user_agent.dart +++ b/packages/neon/neon/lib/src/utils/user_agent.dart @@ -1,8 +1,11 @@ import 'package:meta/meta.dart'; import 'package:package_info_plus/package_info_plus.dart'; -late String? _userAgent; +String? _userAgent; +/// Sets the user agent. +/// +/// It can be accessed with [neonUserAgent]. @internal void buildUserAgent(final PackageInfo packageInfo) { var buildNumber = packageInfo.buildNumber; @@ -12,5 +15,13 @@ void buildUserAgent(final PackageInfo packageInfo) { _userAgent = 'Neon ${packageInfo.version}+$buildNumber'; } +/// Gets the current user agent. +/// +/// It must be set by calling [buildUserAgent] before. If not set a [StateError] will be thrown. @internal -String get neonUserAgent => _userAgent!; +String get neonUserAgent { + if (_userAgent == null) { + throw StateError('The user agent has not been set up. Please use `buildUserAgent` before.'); + } + return _userAgent!; +}