-
Notifications
You must be signed in to change notification settings - Fork 6k
apply null safety syntax to mobile dart:ui #18933
Changes from all commits
62ca6b9
866293f
7ff0045
7cf0137
8511602
2c53f9b
b329025
b6aba25
e288fbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| // @dart = 2.9 | ||
|
|
||
| part of dart.ui; | ||
|
|
||
| /// A saved platform message for a channel with its callback. | ||
|
|
@@ -14,12 +15,12 @@ class _StoredMessage { | |
| _StoredMessage(this._data, this._callback); | ||
|
|
||
| /// Representation of the message's payload. | ||
| final ByteData/*?*/ _data; | ||
| ByteData/*?*/ get data => _data; | ||
| final ByteData? _data; | ||
| ByteData? get data => _data; | ||
|
|
||
| /// Callback to be called when the message is received. | ||
| final PlatformMessageResponseCallback/*!*/ _callback; | ||
| PlatformMessageResponseCallback/*!*/ get callback => _callback; | ||
| final PlatformMessageResponseCallback _callback; | ||
| PlatformMessageResponseCallback get callback => _callback; | ||
| } | ||
|
|
||
| /// A fixed-size circular queue. | ||
|
|
@@ -43,7 +44,7 @@ class _RingBuffer<T> { | |
|
|
||
| /// A callback that get's called when items are ejected from the [_RingBuffer] | ||
| /// by way of an overflow or a resizing. | ||
| Function(T) _dropItemCallback; | ||
| Function(T)? _dropItemCallback; | ||
| set dropItemCallback(Function(T) callback) { | ||
| _dropItemCallback = callback; | ||
| } | ||
|
|
@@ -60,7 +61,7 @@ class _RingBuffer<T> { | |
| } | ||
|
|
||
| /// Returns null when empty. | ||
| T pop() { | ||
| T? pop() { | ||
| return _queue.isEmpty ? null : _queue.removeFirst(); | ||
| } | ||
|
|
||
|
|
@@ -70,9 +71,7 @@ class _RingBuffer<T> { | |
| int result = 0; | ||
| while (_queue.length > lengthLimit) { | ||
| final T item = _queue.removeFirst(); | ||
| if (_dropItemCallback != null) { | ||
| _dropItemCallback(item); | ||
| } | ||
| _dropItemCallback?.call(item); | ||
| result += 1; | ||
| } | ||
| return result; | ||
|
|
@@ -86,7 +85,7 @@ class _RingBuffer<T> { | |
| } | ||
|
|
||
| /// Signature for [ChannelBuffers.drain]. | ||
| typedef DrainChannelCallback = Future<void>/*!*/ Function(ByteData/*?*/, PlatformMessageResponseCallback/*!*/); | ||
| typedef DrainChannelCallback = Future<void> Function(ByteData?, PlatformMessageResponseCallback); | ||
|
|
||
| /// Storage of channel messages until the channels are completely routed, | ||
| /// i.e. when a message handler is attached to the channel on the framework side. | ||
|
|
@@ -116,8 +115,8 @@ class ChannelBuffers { | |
| static const String kControlChannelName = 'dev.flutter/channel-buffers'; | ||
|
|
||
| /// A mapping between a channel name and its associated [_RingBuffer]. | ||
| final Map<String, _RingBuffer<_StoredMessage>> _messages = | ||
| <String, _RingBuffer<_StoredMessage>>{}; | ||
| final Map<String, _RingBuffer<_StoredMessage>?> _messages = | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maps are tricky in that even if you say the values are non-null
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To answer the question: Yes, it can have Whether you want or need that is up to you. |
||
| <String, _RingBuffer<_StoredMessage>?>{}; | ||
|
|
||
| _RingBuffer<_StoredMessage> _makeRingBuffer(int size) { | ||
| final _RingBuffer<_StoredMessage> result = _RingBuffer<_StoredMessage>(size); | ||
|
|
@@ -130,8 +129,8 @@ class ChannelBuffers { | |
| } | ||
|
|
||
| /// Returns true on overflow. | ||
| bool/*!*/ push(String/*!*/ channel, ByteData/*?*/ data, PlatformMessageResponseCallback/*!*/ callback) { | ||
| _RingBuffer<_StoredMessage> queue = _messages[channel]; | ||
| bool push(String channel, ByteData? data, PlatformMessageResponseCallback callback) { | ||
| _RingBuffer<_StoredMessage>? queue = _messages[channel]; | ||
| if (queue == null) { | ||
| queue = _makeRingBuffer(kDefaultBufferSize); | ||
| _messages[channel] = queue; | ||
|
|
@@ -150,23 +149,23 @@ class ChannelBuffers { | |
| } | ||
|
|
||
| /// Returns null on underflow. | ||
| _StoredMessage _pop(String channel) { | ||
| final _RingBuffer<_StoredMessage> queue = _messages[channel]; | ||
| final _StoredMessage result = queue?.pop(); | ||
| _StoredMessage? _pop(String channel) { | ||
| final _RingBuffer<_StoredMessage>? queue = _messages[channel]; | ||
| final _StoredMessage? result = queue?.pop(); | ||
| return result; | ||
| } | ||
|
|
||
| bool _isEmpty(String channel) { | ||
| final _RingBuffer<_StoredMessage> queue = _messages[channel]; | ||
| return (queue == null) ? true : queue.isEmpty; | ||
| final _RingBuffer<_StoredMessage>? queue = _messages[channel]; | ||
| return queue == null || queue.isEmpty; | ||
| } | ||
|
|
||
| /// Changes the capacity of the queue associated with the given channel. | ||
| /// | ||
| /// This could result in the dropping of messages if newSize is less | ||
| /// than the current length of the queue. | ||
| void _resize(String channel, int newSize) { | ||
| _RingBuffer<_StoredMessage> queue = _messages[channel]; | ||
| _RingBuffer<_StoredMessage>? queue = _messages[channel]; | ||
| if (queue == null) { | ||
| queue = _makeRingBuffer(newSize); | ||
| _messages[channel] = queue; | ||
|
|
@@ -182,9 +181,9 @@ class ChannelBuffers { | |
| /// | ||
| /// This should be called once a channel is prepared to handle messages | ||
| /// (i.e. when a message handler is setup in the framework). | ||
| Future<void>/*!*/ drain(String/*!*/ channel, DrainChannelCallback/*!*/ callback) async { | ||
| Future<void> drain(String channel, DrainChannelCallback callback) async { | ||
| while (!_isEmpty(channel)) { | ||
| final _StoredMessage message = _pop(channel); | ||
| final _StoredMessage message = _pop(channel)!; | ||
| await callback(message.data, message.callback); | ||
| } | ||
| } | ||
|
|
@@ -204,7 +203,7 @@ class ChannelBuffers { | |
| /// Arity: 2 | ||
| /// Format: `resize\r<channel name>\r<new size>` | ||
| /// Description: Allows you to set the size of a channel's buffer. | ||
| void handleMessage(ByteData/*!*/ data) { | ||
| void handleMessage(ByteData data) { | ||
| final List<String> command = _getString(data).split('\r'); | ||
| if (command.length == /*arity=*/2 + 1 && command[0] == 'resize') { | ||
| _resize(command[1], int.parse(command[2])); | ||
|
|
@@ -220,4 +219,4 @@ class ChannelBuffers { | |
| /// | ||
| /// See also: | ||
| /// * [BinaryMessenger] - The place where ChannelBuffers are typically read. | ||
| final ChannelBuffers/*!*/ channelBuffers = ChannelBuffers(); | ||
| final ChannelBuffers channelBuffers = ChannelBuffers(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
Tis already nullable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC
Tis potentially non-nullable, so it could beint?. The type system will collapse muliplte??to?, so essentially this forces nullability even a concrete instance ofTis non-nullable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️ What Jonah says. But also, I'm not too worried about what happens inside private classes, as long as there are not breakages or performance regressions. We can revisit specific cases later, when everything uses the same syntax.