Skip to content

Commit abeea0f

Browse files
DevNicohaarts
authored andcommitted
Sound null safety
1 parent ec0e159 commit abeea0f

16 files changed

+68
-65
lines changed

.github/workflows/dart.yml

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

1414
container:
15-
image: google/dart:latest
15+
image: google/dart:2.12-beta
1616

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

lib/src/ansi_color.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class AnsiColor {
66
/// Reset all colors and options for current SGRs to terminal defaults.
77
static const ansiDefault = '${ansiEsc}0m';
88

9-
final int fg;
10-
final int bg;
9+
final int? fg;
10+
final int? bg;
1111
final bool color;
1212

1313
AnsiColor.none()

lib/src/filters/development_filter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DevelopmentFilter extends LogFilter {
1010
bool shouldLog(LogEvent event) {
1111
var shouldLog = false;
1212
assert(() {
13-
if (event.level.index >= level.index) {
13+
if (event.level.index >= level!.index) {
1414
shouldLog = true;
1515
}
1616
return true;

lib/src/filters/production_filter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import 'package:logger/src/log_filter.dart';
55
class ProductionFilter extends LogFilter {
66
@override
77
bool shouldLog(LogEvent event) {
8-
return event.level.index >= level.index;
8+
return event.level.index >= level!.index;
99
}
1010
}

lib/src/log_filter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:logger/src/logger.dart';
55
/// You can implement your own `LogFilter` or use [DevelopmentFilter].
66
/// Every implementation should consider [Logger.level].
77
abstract class LogFilter {
8-
Level level;
8+
Level? level;
99
void init() {}
1010

1111
/// Is called every time a new log message is sent and decides if

lib/src/logger.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LogEvent {
2121
final Level level;
2222
final dynamic message;
2323
final dynamic error;
24-
final StackTrace stackTrace;
24+
final StackTrace? stackTrace;
2525

2626
LogEvent(this.level, this.message, this.error, this.stackTrace);
2727
}
@@ -57,10 +57,10 @@ class Logger {
5757
/// defaults: [PrettyPrinter], [DevelopmentFilter] and [ConsoleOutput] will be
5858
/// used.
5959
Logger({
60-
LogFilter filter,
61-
LogPrinter printer,
62-
LogOutput output,
63-
Level level,
60+
LogFilter? filter,
61+
LogPrinter? printer,
62+
LogOutput? output,
63+
Level? level,
6464
}) : _filter = filter ?? DevelopmentFilter(),
6565
_printer = printer ?? PrettyPrinter(),
6666
_output = output ?? ConsoleOutput() {
@@ -71,38 +71,38 @@ class Logger {
7171
}
7272

7373
/// Log a message at level [Level.verbose].
74-
void v(dynamic message, [dynamic error, StackTrace stackTrace]) {
74+
void v(dynamic message, [dynamic error, StackTrace? stackTrace]) {
7575
log(Level.verbose, message, error, stackTrace);
7676
}
7777

7878
/// Log a message at level [Level.debug].
79-
void d(dynamic message, [dynamic error, StackTrace stackTrace]) {
79+
void d(dynamic message, [dynamic error, StackTrace? stackTrace]) {
8080
log(Level.debug, message, error, stackTrace);
8181
}
8282

8383
/// Log a message at level [Level.info].
84-
void i(dynamic message, [dynamic error, StackTrace stackTrace]) {
84+
void i(dynamic message, [dynamic error, StackTrace? stackTrace]) {
8585
log(Level.info, message, error, stackTrace);
8686
}
8787

8888
/// Log a message at level [Level.warning].
89-
void w(dynamic message, [dynamic error, StackTrace stackTrace]) {
89+
void w(dynamic message, [dynamic error, StackTrace? stackTrace]) {
9090
log(Level.warning, message, error, stackTrace);
9191
}
9292

9393
/// Log a message at level [Level.error].
94-
void e(dynamic message, [dynamic error, StackTrace stackTrace]) {
94+
void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
9595
log(Level.error, message, error, stackTrace);
9696
}
9797

9898
/// Log a message at level [Level.wtf].
99-
void wtf(dynamic message, [dynamic error, StackTrace stackTrace]) {
99+
void wtf(dynamic message, [dynamic error, StackTrace? stackTrace]) {
100100
log(Level.wtf, message, error, stackTrace);
101101
}
102102

103103
/// Log a message with [level].
104104
void log(Level level, dynamic message,
105-
[dynamic error, StackTrace stackTrace]) {
105+
[dynamic error, StackTrace? stackTrace]) {
106106
if (!_active) {
107107
throw ArgumentError('Logger has already been closed.');
108108
} else if (error != null && error is StackTrace) {

lib/src/outputs/file_output.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class FileOutput extends LogOutput {
99
final File file;
1010
final bool overrideExisting;
1111
final Encoding encoding;
12-
IOSink _sink;
12+
IOSink? _sink;
1313

1414
FileOutput({
15-
this.file,
15+
required this.file,
1616
this.overrideExisting = false,
1717
this.encoding = utf8,
1818
});
@@ -27,12 +27,12 @@ class FileOutput extends LogOutput {
2727

2828
@override
2929
void output(OutputEvent event) {
30-
_sink.writeAll(event.lines, '\n');
30+
_sink?.writeAll(event.lines, '\n');
3131
}
3232

3333
@override
3434
void destroy() async {
35-
await _sink.flush();
36-
await _sink.close();
35+
await _sink?.flush();
36+
await _sink?.close();
3737
}
3838
}

lib/src/outputs/memory_output.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class MemoryOutput extends LogOutput {
99
final int bufferSize;
1010

1111
/// A secondary [LogOutput] to also received events.
12-
final LogOutput secondOutput;
12+
final LogOutput? secondOutput;
1313

1414
/// The buffer of events.
1515
final ListQueue<OutputEvent> buffer;
@@ -25,8 +25,6 @@ class MemoryOutput extends LogOutput {
2525

2626
buffer.add(event);
2727

28-
if (secondOutput != null) {
29-
secondOutput.output(event);
30-
}
28+
secondOutput?.output(event);
3129
}
3230
}

lib/src/outputs/multi_output.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import 'package:logger/src/logger.dart';
33

44
/// Logs simultaneously to multiple [LogOutput] outputs.
55
class MultiOutput extends LogOutput {
6-
List<LogOutput> _outputs;
6+
late List<LogOutput> _outputs;
77

8-
MultiOutput(List<LogOutput> outputs) {
8+
MultiOutput(List<LogOutput?>? outputs) {
99
_outputs = _normalizeOutputs(outputs);
1010
}
11-
12-
List<LogOutput> _normalizeOutputs(List<LogOutput> outputs) {
13-
if (outputs == null) return [];
14-
15-
outputs.removeWhere((o) => o == null);
16-
17-
return outputs;
11+
List<LogOutput> _normalizeOutputs(List<LogOutput?>? outputs) {
12+
final normalizedOutputs = <LogOutput>[];
13+
14+
if (outputs != null) {
15+
for (final output in outputs) {
16+
if (output != null) {
17+
normalizedOutputs.add(output);
18+
}
19+
}
20+
}
21+
22+
return normalizedOutputs;
1823
}
1924

2025
@override

lib/src/outputs/stream_output.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:logger/src/logger.dart';
44
import 'package:logger/src/log_output.dart';
55

66
class StreamOutput extends LogOutput {
7-
StreamController<List<String>> _controller;
7+
late StreamController<List<String>> _controller;
88
bool _shouldForward = false;
99

1010
StreamOutput() {

0 commit comments

Comments
 (0)