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()
.
2
Answers
You can initialize
your variable like this if you are planning to assign it later
Or
There are cases where you don’t need to explicitly use
late
even with non-nullable variables with no initial value. For example:or:
In those cases, through control flow analysis, the compiler can guarantee that all code paths result in
x
being initialized before it’s used.The problem is that the semantics of
setUp()
being called beforetest()
(or more precisely, thatsetUp
‘s callback is executed beforetest
‘s callback) is the behavior described by the test framework, not by the language. It’s non-trivial for the compiler to determine that thesetUp
callback is guaranteed to be executed before thetest
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. Usinglate
means that you personally guarantee that the variable will be initialized before it’s used.