Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 62d73d9

Browse files
authored
update for null safety (#145)
update for null safety
1 parent 9f30bd0 commit 62d73d9

File tree

12 files changed

+87
-87
lines changed

12 files changed

+87
-87
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212

1313
container:
14-
image: google/dart:beta
14+
image: google/dart:dev
1515

1616
steps:
1717
- uses: actions/checkout@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# https://dart.dev/guides/libraries/private-files
22
.packages
33
.dart_tool/
4+
.idea/
45
pubspec.lock

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.0.0-nullsafety
2+
- Updated to support 2.12.0 and null safety.
3+
14
## 3.4.2
25
- A number of cleanups to improve the package health score.
36

example/example.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ import 'dart:html';
99

1010
import 'package:usage/usage_html.dart';
1111

12-
Analytics _analytics;
13-
String _lastUa;
12+
Analytics? _analytics;
13+
String? _lastUa;
1414
int _count = 0;
1515

1616
void main() {
17-
querySelector('#foo').onClick.listen((_) => _handleFoo());
18-
querySelector('#bar').onClick.listen((_) => _handleBar());
19-
querySelector('#page').onClick.listen((_) => _changePage());
17+
querySelector('#foo')!.onClick.listen((_) => _handleFoo());
18+
querySelector('#bar')!.onClick.listen((_) => _handleBar());
19+
querySelector('#page')!.onClick.listen((_) => _changePage());
2020
}
2121

22-
String _ua() => (querySelector('#ua') as InputElement).value.trim();
22+
String _ua() => (querySelector('#ua') as InputElement).value!.trim();
2323

2424
Analytics getAnalytics() {
2525
if (_analytics == null || _lastUa != _ua()) {
2626
_lastUa = _ua();
27-
_analytics = AnalyticsHtml(_lastUa, 'Test app', '1.0');
28-
_analytics.sendScreenView(window.location.pathname);
27+
_analytics = AnalyticsHtml(_lastUa!, 'Test app', '1.0');
28+
_analytics!.sendScreenView(window.location.pathname!);
2929
}
3030

31-
return _analytics;
31+
return _analytics!;
3232
}
3333

3434
void _handleFoo() {
@@ -44,5 +44,5 @@ void _handleBar() {
4444
void _changePage() {
4545
var analytics = getAnalytics();
4646
window.history.pushState(null, 'new page', '${++_count}.html');
47-
analytics.sendScreenView(window.location.pathname);
47+
analytics.sendScreenView(window.location.pathname!);
4848
}

lib/src/usage_impl.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ String postEncode(Map<String, dynamic> map) {
2525
class ThrottlingBucket {
2626
final int startingCount;
2727
int drops;
28-
int _lastReplenish;
28+
late int _lastReplenish;
2929

30-
ThrottlingBucket(this.startingCount) {
31-
drops = startingCount;
30+
ThrottlingBucket(this.startingCount) : drops = startingCount {
3231
_lastReplenish = DateTime.now().millisecondsSinceEpoch;
3332
}
3433

@@ -61,9 +60,9 @@ class AnalyticsImpl implements Analytics {
6160
@override
6261
final String trackingId;
6362
@override
64-
final String applicationName;
63+
final String? applicationName;
6564
@override
66-
final String applicationVersion;
65+
final String? applicationVersion;
6766

6867
final PersistentProperties properties;
6968
final PostHandler postHandler;
@@ -76,22 +75,20 @@ class AnalyticsImpl implements Analytics {
7675
@override
7776
AnalyticsOpt analyticsOpt = AnalyticsOpt.optOut;
7877

79-
String _url;
78+
late String _url;
8079

8180
final StreamController<Map<String, dynamic>> _sendController =
8281
StreamController.broadcast(sync: true);
8382

8483
AnalyticsImpl(this.trackingId, this.properties, this.postHandler,
85-
{this.applicationName, this.applicationVersion, String analyticsUrl}) {
86-
assert(trackingId != null);
87-
84+
{this.applicationName, this.applicationVersion, String? analyticsUrl}) {
8885
if (applicationName != null) setSessionValue('an', applicationName);
8986
if (applicationVersion != null) setSessionValue('av', applicationVersion);
9087

9188
_url = analyticsUrl ?? _defaultAnalyticsUrl;
9289
}
9390

94-
bool _firstRun;
91+
bool? _firstRun;
9592

9693
@override
9794
bool get firstRun {
@@ -103,7 +100,7 @@ class AnalyticsImpl implements Analytics {
103100
}
104101
}
105102

106-
return _firstRun;
103+
return _firstRun!;
107104
}
108105

109106
@override
@@ -120,7 +117,7 @@ class AnalyticsImpl implements Analytics {
120117
}
121118

122119
@override
123-
Future sendScreenView(String viewName, {Map<String, String> parameters}) {
120+
Future sendScreenView(String viewName, {Map<String, String>? parameters}) {
124121
var args = <String, dynamic>{'cd': viewName};
125122
if (parameters != null) {
126123
args.addAll(parameters);
@@ -130,7 +127,7 @@ class AnalyticsImpl implements Analytics {
130127

131128
@override
132129
Future sendEvent(String category, String action,
133-
{String label, int value, Map<String, String> parameters}) {
130+
{String? label, int? value, Map<String, String>? parameters}) {
134131
var args = <String, dynamic>{'ec': category, 'ea': action};
135132
if (label != null) args['el'] = label;
136133
if (value != null) args['ev'] = value;
@@ -148,7 +145,7 @@ class AnalyticsImpl implements Analytics {
148145

149146
@override
150147
Future sendTiming(String variableName, int time,
151-
{String category, String label}) {
148+
{String? category, String? label}) {
152149
var args = <String, dynamic>{'utv': variableName, 'utt': time};
153150
if (label != null) args['utl'] = label;
154151
if (category != null) args['utc'] = category;
@@ -157,12 +154,12 @@ class AnalyticsImpl implements Analytics {
157154

158155
@override
159156
AnalyticsTimer startTimer(String variableName,
160-
{String category, String label}) {
157+
{String? category, String? label}) {
161158
return AnalyticsTimer(this, variableName, category: category, label: label);
162159
}
163160

164161
@override
165-
Future sendException(String description, {bool fatal}) {
162+
Future sendException(String description, {bool? fatal}) {
166163
// We trim exceptions to a max length; google analytics will apply it's own
167164
// truncation, likely around 150 chars or so.
168165
const maxExceptionLength = 1000;
@@ -201,7 +198,7 @@ class AnalyticsImpl implements Analytics {
201198
Stream<Map<String, dynamic>> get onSend => _sendController.stream;
202199

203200
@override
204-
Future waitForLastPing({Duration timeout}) {
201+
Future waitForLastPing({Duration? timeout}) {
205202
Future f = Future.wait(_futures).catchError((e) => null);
206203

207204
if (timeout != null) {
@@ -268,6 +265,7 @@ abstract class PersistentProperties {
268265
PersistentProperties(this.name);
269266

270267
dynamic operator [](String key);
268+
271269
void operator []=(String key, dynamic value);
272270

273271
/// Re-read settings from the backing store. This may be a no-op on some

lib/src/usage_impl_html.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,39 @@ import 'usage_impl.dart';
1515
class AnalyticsHtml extends AnalyticsImpl {
1616
AnalyticsHtml(
1717
String trackingId, String applicationName, String applicationVersion,
18-
{String analyticsUrl})
18+
{String? analyticsUrl})
1919
: super(trackingId, HtmlPersistentProperties(applicationName),
2020
HtmlPostHandler(),
2121
applicationName: applicationName,
2222
applicationVersion: applicationVersion,
2323
analyticsUrl: analyticsUrl) {
24-
var screenWidth = window.screen.width;
25-
var screenHeight = window.screen.height;
24+
var screenWidth = window.screen!.width;
25+
var screenHeight = window.screen!.height;
2626

2727
setSessionValue('sr', '${screenWidth}x$screenHeight');
28-
setSessionValue('sd', '${window.screen.pixelDepth}-bits');
28+
setSessionValue('sd', '${window.screen!.pixelDepth}-bits');
2929
setSessionValue('ul', window.navigator.language);
3030
}
3131
}
3232

3333
typedef HttpRequestor = Future<HttpRequest> Function(String url,
34-
{String method, dynamic sendData});
34+
{String? method, dynamic sendData});
3535

3636
class HtmlPostHandler extends PostHandler {
37-
final HttpRequestor mockRequestor;
37+
final HttpRequestor? mockRequestor;
3838

3939
HtmlPostHandler({this.mockRequestor});
4040

4141
@override
4242
Future sendPost(String url, Map<String, dynamic> parameters) {
43-
var viewportWidth = document.documentElement.clientWidth;
44-
var viewportHeight = document.documentElement.clientHeight;
43+
var viewportWidth = document.documentElement!.clientWidth;
44+
var viewportHeight = document.documentElement!.clientHeight;
4545

4646
parameters['vp'] = '${viewportWidth}x$viewportHeight';
4747

4848
var data = postEncode(parameters);
49-
var requestor = mockRequestor ?? HttpRequest.request;
49+
Future<HttpRequest> Function(String, {String method, dynamic sendData})
50+
requestor = mockRequestor ?? HttpRequest.request;
5051
return requestor(url, method: 'POST', sendData: data).catchError((e) {
5152
// Catch errors that can happen during a request, but that we can't do
5253
// anything about, e.g. a missing internet connection.
@@ -58,7 +59,7 @@ class HtmlPostHandler extends PostHandler {
5859
}
5960

6061
class HtmlPersistentProperties extends PersistentProperties {
61-
Map _map;
62+
late Map _map;
6263

6364
HtmlPersistentProperties(String name) : super(name) {
6465
var str = window.localStorage[name];

lib/src/usage_impl_io.dart

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import 'usage_impl.dart';
2424
class AnalyticsIO extends AnalyticsImpl {
2525
AnalyticsIO(
2626
String trackingId, String applicationName, String applicationVersion,
27-
{String analyticsUrl, Directory documentDirectory})
27+
{String? analyticsUrl, Directory? documentDirectory})
2828
: super(
2929
trackingId,
3030
IOPersistentProperties(applicationName,
@@ -75,9 +75,9 @@ String getDartVersion() {
7575

7676
class IOPostHandler extends PostHandler {
7777
final String _userAgent;
78-
final HttpClient mockClient;
78+
final HttpClient? mockClient;
7979

80-
HttpClient _client;
80+
HttpClient? _client;
8181

8282
IOPostHandler({this.mockClient}) : _userAgent = _createUserAgent();
8383

@@ -87,11 +87,11 @@ class IOPostHandler extends PostHandler {
8787

8888
if (_client == null) {
8989
_client = mockClient ?? HttpClient();
90-
_client.userAgent = _userAgent;
90+
_client!.userAgent = _userAgent;
9191
}
9292

9393
try {
94-
var req = await _client.postUrl(Uri.parse(url));
94+
var req = await _client!.postUrl(Uri.parse(url));
9595
req.write(data);
9696
var response = await req.close();
9797
await response.drain();
@@ -108,10 +108,10 @@ class IOPostHandler extends PostHandler {
108108
JsonEncoder _jsonEncoder = JsonEncoder.withIndent(' ');
109109

110110
class IOPersistentProperties extends PersistentProperties {
111-
File _file;
112-
Map _map;
111+
late File _file;
112+
late Map _map;
113113

114-
IOPersistentProperties(String name, {String documentDirPath}) : super(name) {
114+
IOPersistentProperties(String name, {String? documentDirPath}) : super(name) {
115115
var fileName = '.${name.replaceAll(' ', '_')}';
116116
documentDirPath ??= userHomeDir();
117117
_file = File(path.join(documentDirPath, fileName));
@@ -162,18 +162,15 @@ class IOPersistentProperties extends PersistentProperties {
162162

163163
/// Return the string for the platform's locale; return's `null` if the locale
164164
/// can't be determined.
165-
String getPlatformLocale() {
165+
String? getPlatformLocale() {
166166
var locale = Platform.localeName;
167-
if (locale == null) return null;
168167

169-
if (locale != null) {
170-
// Convert `en_US.UTF-8` to `en_US`.
171-
var index = locale.indexOf('.');
172-
if (index != -1) locale = locale.substring(0, index);
168+
// Convert `en_US.UTF-8` to `en_US`.
169+
var index = locale.indexOf('.');
170+
if (index != -1) locale = locale.substring(0, index);
173171

174-
// Convert `en_US` to `en-us`.
175-
locale = locale.replaceAll('_', '-').toLowerCase();
176-
}
172+
// Convert `en_US` to `en-us`.
173+
locale = locale.replaceAll('_', '-').toLowerCase();
177174

178175
return locale;
179176
}

0 commit comments

Comments
 (0)