Home > Net >  Late variable in Dart
Late variable in Dart

Time:04-27

I'm new to use Websocket in Flutter.

So I was reading some post (https://blog.logrocket.com/using-websockets-flutter/) to understand how to use websocket, and I have some question.

The guy in this post declare _etheWebsocket,_btcWebsocket variables as late as below.

class CoinbaseProvider {
  late final WebSocketChannel _ethWebsocket;
  late final WebSocketChannel _btcWebsocket;

  CoinbaseProvider()
      : _ethWebsocket = WebSocketChannel.connect(
          Uri.parse('wss://ws-feed.pro.coinbase.com'),
        ),
        _btcWebsocket = WebSocketChannel.connect(
          Uri.parse('wss://ws-feed.pro.coinbase.com'),
        );

But why not just declare without late like below?? What is the reason have to declare WebsocketChannel as late.

class CoinbaseProvider {
  final WebSocketChannel _ethWebsocket = WebSocketChannel.connect(
    Uri.parse('wss://ws-feed.pro.coinbase.com'),
  );
  final WebSocketChannel _btcWebsocket = WebSocketChannel.connect(
    Uri.parse('wss://ws-feed.pro.coinbase.com'),
  );

  CoinbaseProvider();

CodePudding user response:

Short explanation

You declaration looks better, since in your context you have the values to the variables _ethWebsocket and _btcWebsocket

But why? a dive into late and null-safety features

What's null-safety in Dart? This is a recent released feature (2021) that in summary it prevent us, developers, to create variables of a type that can be implicity null, then avoiding runtime errors as TypeError: can't read property '...' of null

But in summary, unlike before null-safety, now:

All variables can't be null implicity by default!

Show me the code

  • Before null-safety:

This snippet below is a valid statement before null-safety, this variable can be a String 'hi!' or null

String myVariable; // null by default
  • After null-safety:

The same snippet now is a invalid statement, the variable should be initialized because her can't be null

String myVariable; // Error!

To declare a variable that can be null by default, as like before the null-safety update you should write as:

String? myVariable; // Note the '?' after the Type
  • But what if you want to declare variable without the value and without explicitly declaring that it can be null?

The answer is late!

late is just a Dart language feature to allow you say to the compiler: "Hey, I don't have the value of this variable right now, but I promise you I'll set her somewhere else before using it, so her can't be null"

late String myVariable;

print(myVariable); // Throws an LateInitializationError instead of printing 'null'

Conclusion

Given these features and the null-safety use them to avoid wasting time with null pointer exceptions and improve your code typing.

So there's no right or better way, all depends on the context.

CodePudding user response:

For the exact example you used I would say they are identical, and your version would be better. My guess it that they made it like that, is to allow adding additional constructors, maybe like

CoinbaseProvider.customUrl(String eth, String btc)
  : _ethWebsocket = WebSocketChannel.connect(
      Uri.parse(eth),
    ),
    _btcWebsocket = WebSocketChannel.connect(
      Uri.parse(btc),
    );
  • Related