我正在升级一个基于 Flutter 框架的个人包。我在 Flutter Text 小部件源代码中注意到这里有一个空检查:
if (textSpan != null) {
properties.add(textSpan!.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
}
Run Code Online (Sandbox Code Playgroud)
但是,textSpan!仍然使用!运算符。不textSpan应该在不必使用!运算符的情况下提升为不可为空的类型吗?但是,尝试删除运算符会出现以下错误:
Run Code Online (Sandbox Code Playgroud)An expression whose value can be 'null' must be null-checked before it can be dereferenced. Try checking that the value isn't 'null' before dereferencing it.
这是一个独立的示例:
An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
Run Code Online (Sandbox Code Playgroud)
我收到一个编译时错误:
错误:“字符串?”类型的值 …
为什么在 Dart 的 null 安全之后无法访问 List() 构造函数?
// Compile time error: 'List' is deprecated and shouldn't be used.
List<int> foo = List();
Run Code Online (Sandbox Code Playgroud)
但是,您仍然可以这样做:
List<int> foo = []; // No error
Run Code Online (Sandbox Code Playgroud)
那么,两者有什么区别呢?他们都应该显示错误或不显示错误。
我有类似于此示例代码的代码(不要介意它没有意义):
void foo(Map<int, String> myMap) {
String s = myMap[1];
}
Run Code Online (Sandbox Code Playgroud)
dart 分析器向我发出有关该行的警告,String s = myMap[1];并显示以下警告:
“字符串?”类型的值 不能分配给“String”类型的变量。尝试更改变量的类型,或将右侧类型转换为“String”。
我看到这种情况正在发生,因为从地图中检索值可能会导致null. 为什么下面的代码片段给了我同样的警告?
void foo(Map<int, String> myMap) {
if (myMap.containsKey(1)) {
String s = myMap[1];
}
}
Run Code Online (Sandbox Code Playgroud) 这段 Dart 官方视频指出,Dart 所谓的“声音空安全”比 Kotlin 的空安全设计要好,因为它可以根据变量是否被声明为可空来优化代码,其他语言(我假设这是指包括 Kotlin 在内的语言) ) 必须进行运行时检查以确保空值安全。
那么,Dart 做了哪些额外的优化?
它如何与不支持空安全的遗留代码库互操作,同时确保空安全?
language-design dart kotlin kotlin-null-safety dart-null-safety
我有一个空安全库,在示例文件夹中,我使用了以下导入:
import 'package:flutter/material.dart';
Run Code Online (Sandbox Code Playgroud)
但是,短绒给我以下警告:
库 'package:flutter/material.dart' 是遗留的,不应导入到空安全库中。尝试迁移导入的库。import_of_legacy_library_into_null_safe
示例项目的 pubspec.yaml 文件指定了 Dart SDK 的测试版:
environment:
sdk: ">=2.12.0-29.10.beta <3.0.0"
dependencies:
flutter:
sdk: flutter
my_library:
path: ../
Run Code Online (Sandbox Code Playgroud)
material.dart 现在不是已经转换了吗?我需要在 pubspec.yaml 中添加其他内容还是等待稳定版本发布?
迁移到空安全后显示此错误。我现在应该怎么做?
Widget chatMessages() {
return StreamBuilder(
stream: messageStream,
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
padding: EdgeInsets.only(bottom: 70, top: 16),
itemCount: snapshot.data.docs.length,
reverse: true,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.docs[index];
return chatMessageTitle(
ds["message"], myUserName == ds["sendBy"]);
})
: Center(child: CircularProgressIndicator());
});
}
Run Code Online (Sandbox Code Playgroud)
添加空检查 (!) 后显示此错误<the getter 'docs' is not Define for the type of object>
itemCount: snapshot.data!.docs.length,
reverse: true,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data!.docs[index];
Run Code Online (Sandbox Code Playgroud) 谁能解释一下使用它之间的区别:
final block = blocks?.first;
Run Code Online (Sandbox Code Playgroud)
和这个:
final block = blocks!.first;
Run Code Online (Sandbox Code Playgroud)
哪里blocks:
List<Block>? blocks
Run Code Online (Sandbox Code Playgroud) 我已经添加// @dart=2.9到我的所有文件中,但build_runner不会在不抛出此错误的情况下做它的事情:
Warning: Operand of null-aware operation '?.' has type 'SendPort' which excludes null. - 'SendPort' is from 'dart:isolate'. sendPort?.send(result); ^Error: Cannot run with sound null safety, because the following dependencies don't support null safety:
- package:build_runner_core
- package:json_serializable
- package:moor_generator
- package:objectbox_generator
- package:retrofit_generator
- package:source_gen
- package:build_config
- package:build_runner
- package:build
- package:json_annotation
- package:glob
- package:pool
- package:crypto
- package:logging
- package:watcher
- package:build_resolvers
- package:timing
- package:graphs
- package:package_config
- package:yaml
- …Run Code Online (Sandbox Code Playgroud) class Foo {
int count; // Error
void bar() => count = 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我已经在bar方法中初始化它时看到错误?如果count被标记为 ,我可以理解这个错误final。
运行 flutter pub run build_runner build 时出现此错误。
Failed to build build_runner:build_runner:
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:258:50: Error: The property 'displayString' is defined in multiple extensions for 'TargetKind' and neither is more specific.
- 'TargetKind' is from 'package:meta/meta_meta.dart' ('../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart').
Try using an explicit extension application of the wanted extension or hiding unwanted extensions from scope.
var kindNames = kinds.map((kind) => kind.displayString).toList()
^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:1950:14: Context: This is one of the extension members.
String get displayString {
^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart:91:14: Context: This is one of the extension members. …Run Code Online (Sandbox Code Playgroud) dart ×10
dart-null-safety ×10
flutter ×6
dart-pub ×2
build-runner ×1
firebase ×1
flutter-hive ×1
kotlin ×1