@@ -8,7 +8,7 @@ class Cache<T> {
88 late Duration _duration;
99
1010 /// The cache itself
11- final List <CacheItem <T >> _cache = [] ;
11+ List <CacheItem <T >>? _cache;
1212
1313 /// The timer which handles the loop
1414 Timer ? _timer;
@@ -19,8 +19,12 @@ class Cache<T> {
1919
2020 /// Gets a cached item with the given key
2121 T ? get (String key) {
22+ if (_cache == null ) {
23+ return null ;
24+ }
25+
2226 final CacheItem <T >? cacheItem =
23- _cache.firstWhereOrNull ((element) => element.key == key);
27+ _cache! .firstWhereOrNull ((element) => element.key == key);
2428
2529 // If there is no item cached
2630 if (cacheItem == null ) {
@@ -38,15 +42,18 @@ class Cache<T> {
3842
3943 /// Gets all cached items
4044 List <T >? getAll () {
41- List <T >? list;
45+ if (_cache == null ) {
46+ return null ;
47+ }
48+
49+ final List <T > list = [];
4250
43- for (int index = _cache.length - 1 ; index >= 0 ; index-- ) {
44- final item = _cache[index];
51+ for (int index = _cache! .length - 1 ; index >= 0 ; index-- ) {
52+ final item = _cache! [index];
4553
4654 if (_isItemOutdated (item)) {
4755 removeItem (item);
4856 } else {
49- list ?? = [];
5057 list.add (item.item);
5158 }
5259 }
@@ -55,15 +62,18 @@ class Cache<T> {
5562 }
5663
5764 List <T >? getWhere (bool Function (CacheItem <T >) function) {
58- final items = _cache.where (function);
65+ if (_cache == null ) {
66+ return null ;
67+ }
68+
69+ final items = _cache! .where (function);
5970
60- List <T >? list;
71+ final List <T > list = [] ;
6172
6273 for (final item in items) {
6374 if (_isItemOutdated (item)) {
6475 removeItem (item);
6576 } else {
66- list ?? = [];
6777 list.add (item.item);
6878 }
6979 }
@@ -78,10 +88,13 @@ class Cache<T> {
7888
7989 /// Inserts an item with the given key and duration into the cache
8090 void putWithDuration (String key, T item, Duration duration) {
91+ // Initialize the cache if needed
92+ _cache ?? = [];
93+
8194 final endTime = _nowInSeconds () + duration.inSeconds;
8295
8396 final CacheItem <T > cacheItem = CacheItem <T >(key, item, endTime);
84- _cache.add (cacheItem);
97+ _cache! .add (cacheItem);
8598
8699 // Try to start the timer if not done yet
87100 _tryStartTimer ();
@@ -95,12 +108,15 @@ class Cache<T> {
95108 /// Inserts many items into the cache with the given duration
96109 void putManyWithDuration (
97110 List <T > items, String Function (T item) getKey, Duration duration) {
111+ // Initialize the cache if needed
112+ _cache ?? = [];
113+
98114 final endTime = _nowInSeconds () + duration.inSeconds;
99115
100116 for (final item in items) {
101117 final key = getKey (item);
102118 final CacheItem <T > cacheItem = CacheItem <T >(key, item, endTime);
103- _cache.add (cacheItem);
119+ _cache! .add (cacheItem);
104120 }
105121
106122 // Try to start the timer if not done yet
@@ -109,17 +125,17 @@ class Cache<T> {
109125
110126 /// Removes the item with the given key from the cache
111127 void remove (String key) {
112- _cache.removeWhere ((element) => element.key == key);
128+ _cache? .removeWhere ((element) => element.key == key);
113129 }
114130
115131 /// Removes the given item from the cache
116132 void removeItem (CacheItem item) {
117- _cache.remove (item);
133+ _cache? .remove (item);
118134 }
119135
120136 /// Removes all items from the cache
121137 void removeAll () {
122- _cache.clear ();
138+ _cache? .clear ();
123139 }
124140
125141 /// Sets a new default duration
@@ -132,15 +148,17 @@ class Cache<T> {
132148 bool _isItemOutdated (CacheItem <T > item) => item.endTime <= _nowInSeconds ();
133149
134150 void _tryStartTimer () {
135- if (_cache.isEmpty || (_timer != null && _timer! .isActive)) {
151+ if (_cache == null ||
152+ _cache! .isEmpty ||
153+ (_timer != null && _timer! .isActive)) {
136154 return ;
137155 }
138156
139157 _timer = Timer .periodic (const Duration (seconds: 10 ), _timerCallBack);
140158 }
141159
142160 void _timerCallBack (Timer timer) {
143- if (_cache.isEmpty) {
161+ if (_cache == null || _cache ! .isEmpty) {
144162 _timer? .cancel ();
145163 return ;
146164 }
0 commit comments