From 1e37e1c18baeaec8a2cbf05ed3a2ad26606b7cc6 Mon Sep 17 00:00:00 2001 From: Elias Yishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 30 Mar 2023 19:27:07 -0400 Subject: [PATCH] Update session.dart (#57) --- pkgs/unified_analytics/lib/src/session.dart | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index 5254a2535..4fc694ddf 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -3,6 +3,7 @@ // BSD-style license that can be found in the LICENSE file. import 'dart:convert'; +import 'dart:io'; import 'package:clock/clock.dart'; import 'package:file/file.dart'; @@ -76,18 +77,24 @@ class Session { /// to date incase another tool is also calling this package and /// making updates to the session file void _refreshSessionData() { - try { + /// Using a nested function here to reduce verbosity + void parseContents() { final String sessionFileContents = _sessionFile.readAsStringSync(); final Map sessionObj = jsonDecode(sessionFileContents); _sessionId = sessionObj['session_id'] as int; _lastPing = sessionObj['last_ping'] as int; + } + + try { + parseContents(); } on FormatException { Initializer.createSessionFile(sessionFile: _sessionFile); - final String sessionFileContents = _sessionFile.readAsStringSync(); - final Map sessionObj = jsonDecode(sessionFileContents); - _sessionId = sessionObj['session_id'] as int; - _lastPing = sessionObj['last_ping'] as int; + parseContents(); + } on PathNotFoundException { + Initializer.createSessionFile(sessionFile: _sessionFile); + + parseContents(); } } }