英文:
How can I fix the error of "unassigned variable" even tho the variable has been initialized?
问题
I have this code:
void main() {
RethinkDb r = RethinkDb();
Connection connection;
UserService sut;
setUp(() async {
connection = await r.connect(host: "127.0.0.1", port: 28015);
await createDb(r, connection);
sut = UserService(r, connection); // second line
});
test('creates a new user document in database', () async {
final user = User(
userName: 'test',
photoUrl: 'url',
active: true,
lastSeen: DateTime.now(),
);
final userWithId = await sut.connect(user); // first line
expect(userWithId.id, isNotEmpty);
});
}
I'm having an error on "first line" that says that the sut
variable must be initialized, but when you look at "second line," you can see that the sut
is indeed initialized, and the setUp()
function is called before the test()
.
英文:
I have this code :
void main() {
RethinkDb r = RethinkDb();
Connection connection;
UserService sut;
setUp(() async {
connection = await r.connect(host: "127.0.0.1", port: 28015);
await createDb(r, connection);
sut = UserService(r, connection); // second line
});
test('creates a new user documenet in database', () async {
final user = User(
userName: 'test',
photoUrl: 'url',
active: true,
lastSeen: DateTime.now(),
);
final userWithId = await sut.connect(user); // first line
expect(userWithId.id, isNotEmpty);
});
}
I'm having an error on "first line" that says that the sut
variable must be initialized, but when you look at "second line", you can see that the sut
is indeed initialized, and the setUp()
function is called before the test()
.
答案1
得分: 1
你可以像这样初始化你的变量,如果你计划稍后赋值:
UserService? sut
或者
late UserService sut
英文:
You can initialize
your variable like this if you are planning to assign it later
UserService? sut
Or
late UserService sut
答案2
得分: 1
以下是翻译好的部分:
有些情况下,即使对于没有初始值的非空变量,您也不需要显式使用 late
。例如:
void main() {
int x;
x = 42;
print(x);
}
或者:
import 'dart:math';
void main() {
int x;
if (Random().nextBool()) {
x = 1;
} else {
x = 0;
}
print(x);
}
在这些情况下,通过控制流分析,编译器可以保证所有代码路径都会在使用 x
之前对其进行初始化。
但是,当您查看 "second line" 时,可以看到 sut
确实被初始化,setUp()
函数在 test()
之前调用。
问题在于 setUp()
在 test()
之前被调用的语义(更确切地说,是 setUp
的 回调 在 test
的 回调 之前执行)是测试框架描述的行为,而不是语言描述的行为。编译器要确定 setUp
回调 保证 在 test
回调之前执行是非常复杂的。确定这一点需要对这些函数的实现执行流分析(以及它们调用的任何函数,依此类推),这可能会非常昂贵。
这就是为什么 late
关键字存在的全部原因:它告诉编译器 您 比它更了解。使用 late
意味着您个人保证在使用它之前会对变量进行初始化。
英文:
There are cases where you don't need to explicitly use late
even with non-nullable variables with no initial value. For example:
void main() {
int x;
x = 42;
print(x);
}
or:
import 'dart:math';
void main() {
int x;
if (Random().nextBool()) {
x = 1;
} else {
x = 0;
}
print(x);
}
In those cases, through control flow analysis, the compiler can guarantee that all code paths result in x
being initialized before it's used.
> but when you look at "second line", you can see that the sut
is indeed initialized, and the setUp()
function is called before the test()
.
The problem is that the semantics of setUp()
being called before test()
(or more precisely, that setUp
's callback is executed before test
's callback) is the behavior described by the test framework, not by the language. It's non-trivial for the compiler to determine that the setUp
callback is guaranteed to be executed before the test
callback. Determining that would involve performing flow analysis on the implementations of those functions (and of any functions they call, and so on), which could be prohibitively expensive.
That's the whole reason why the late
keyword exists: to tell the compiler that you know more than it does. Using late
means that you personally guarantee that the variable will be initialized before it's used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论