dart null 安全和打字问题
我正在尝试学习 Dart,但我很难理解空安全性。我有下面的函数,它是我直接从开发示例中复制的(https://pub.dev/packages/udp )并稍微调整了我的代码。
void udpConnection(port) async {
var sender = await UDP.bind(Endpoint.any(port: Port(65000)));
var dataLength = await sender.send('Hello World!'.codeUnits, Endpoint.broadcast(port: Port(port)));
var receiver = await UDP.bind(Endpoint.loopback(port: Port(65002)));
try {
receiver.asStream(timeout: Duration(seconds: 20)).listen((datagram) {
String s = new String.fromCharCodes(datagram.data);
print(s);
});
} catch(e) {
print(e);
}
// close the UDP instances and their sockets.
sender.close();
receiver.close();
}
但我收到以下错误:
Error: Property 'data' cannot be accessed on 'Datagram?' because it is potentially null.
- 'Datagram' is from 'dart:io'.
Try accessing using ?. instead.
String s = new String.fromCharCodes(datagram.data);
^^^^
但是,如果我执行 String s = new String.fromCharCodes(datagram?.data);
,我收到以下错误:
Error: The argument type 'Uint8List?' can't be assigned to the parameter type 'Iterable<int>' because 'Uint8List?' is nullable and 'Iterable<int>' isn't.
- 'Uint8List' is from 'dart:typed_data'.
- 'Iterable' is from 'dart:core'.
String s = new String.fromCharCodes(datagram?.data);
^
How can I access the data property of the Datagram正确吗?
I'm trying to learn Dart, but I'm having a really hard time wrapping my head around the Null Safety. I have the function below, which I copied straight from the dev example (https://pub.dev/packages/udp) and tweaked for my code just slightly.
void udpConnection(port) async {
var sender = await UDP.bind(Endpoint.any(port: Port(65000)));
var dataLength = await sender.send('Hello World!'.codeUnits, Endpoint.broadcast(port: Port(port)));
var receiver = await UDP.bind(Endpoint.loopback(port: Port(65002)));
try {
receiver.asStream(timeout: Duration(seconds: 20)).listen((datagram) {
String s = new String.fromCharCodes(datagram.data);
print(s);
});
} catch(e) {
print(e);
}
// close the UDP instances and their sockets.
sender.close();
receiver.close();
}
But I get the following error:
Error: Property 'data' cannot be accessed on 'Datagram?' because it is potentially null.
- 'Datagram' is from 'dart:io'.
Try accessing using ?. instead.
String s = new String.fromCharCodes(datagram.data);
^^^^
However, if I do String s = new String.fromCharCodes(datagram?.data);
, I get the following error:
Error: The argument type 'Uint8List?' can't be assigned to the parameter type 'Iterable<int>' because 'Uint8List?' is nullable and 'Iterable<int>' isn't.
- 'Uint8List' is from 'dart:typed_data'.
- 'Iterable' is from 'dart:core'.
String s = new String.fromCharCodes(datagram?.data);
^
How can I access the data property of the Datagram correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果左侧为
null
,条件成员访问运算符 (?.
) 的计算结果为null
。其要点是避免通过尝试访问null
上不存在的成员来生成空指针异常。使用
String.fromCharCodes(datagram?.data)
不会神奇地避免调用String.fromCharCodes
构造函数,并且String.fromCharCodes
期望一个非空参数。您必须为
String.fromCharCodes
提供一个保证不为null
的值,或者必须避免调用它。示例:或者:
如果您还没有阅读过了解 null safety,我强烈建议您阅读已经这样做了。
请注意,
?.
和??
是空感知运算符,但并不是真正的空安全。空安全是指使用类型系统来确保变量在您不希望的情况下不会为空。The conditional member access operator (
?.
) evaluates tonull
if the left-hand-side isnull
. The point of it is to avoid generating a null-pointer exception by attempting to access a non-existent member onnull
.Using
String.fromCharCodes(datagram?.data)
would not magically avoid calling theString.fromCharCodes
constructor, andString.fromCharCodes
expects a non-null argument.You either must give
String.fromCharCodes
a value that you guarantee is notnull
or must avoid calling it. Examples:or:
I strongly recommend reading Understanding null safety if you haven't already done so.
Note that
?.
and??
are null-aware operators but aren't really about null-safety. Null-safety is about using the type system to ensure that variables cannot benull
when you don't expect them to be.