How to use the Assert statement?

Asked

Viewed 1,251 times

3

To Documenting informs that:

During development, use a statement assert for interrupt normal execution if a condition is false.

The instruction assert ensures, for example, the manipulation of values nulos during development, avoiding possible runtime errors.

Example:

void main() {
  Nome(null).imprimir();
}

class Nome {
  Nome(this.nome) : assert(nome != null && nome.isNotEmpty);
  final String nome;

  imprimir(){
    print('Meu nome é $nome');    
  }
}

Print: My name is null /but should generate an Exception

Why this example does not behave as expected?

https://dartpad.dartlang.org/d98ad76b6e985e6cff27ecc7aea07d29

1 answer

4


First of all, let’s just put a little context.

Debugging mechanisms during development

It is often necessary, during the development of an application, to add certain debugging mechanisms in order to be aware if each part of it is functioning properly.

These mechanisms, or tools, can come in a separate way, or even as an already present feature (I think I can say, native) in the language being used to develop the application.

Examples that fit into the "tools apart" group are the tools for testing, in addition to the debugging systems built into Ides (and some text editors).

And an example that fits into the other group is exactly what this is about, that is, the structure assert, which serves, in development mode most of the time, to assign conditions which must become true. And if they don’t, generate an interruption.

Using the structure assert in Dart

As I mentioned above, and even as it says dart documentation, the structure assert can be used in your application for an interruption in execution if an expression returns value boolean phony, that is to say, false. Using even the example given in the question, one can understand this:

void main() {
  Nome(null).imprimir();
}

class Nome {
  Nome(this.nome) : assert(nome != null && nome.isNotEmpty);
  final String nome;

  imprimir(){
    print('Meu nome é $nome');    
  }
}

That is, the structure assert should ensure (and once again remember, during development) that the value passed as argument to the class constructor Nome (and will be kept in the field nome):

  • Is not null (null);
  • And being a string, is not a string empty.

In addition, you can also provide a custom message if the condition is not met, this way:

Nome(this.nome) : assert(nome != null && nome.isNotEmpty, "'nome' está se tornando nulo ou é uma string vazia");

How to perform correctly?

Depending on how the source code will be compiled, different procedures are done, as the documentation says.

Using the tool dartdevc

This tool, as well as the dart2js, is used to convert Dart code into Javascript code, since Dart can also be used in web. The difference is that the dartdevc, as the name may suggest, is used for development only, therefore it leaves the structures assert active. Thus, basically, to compile using this tool, this is used:

dartdevc <nome_do_arquivo_fonte>.dart -o <nome_do_arquivo_resultante>.js

Using the tools dart and dart2js

Already the tools dart and dart2js (the latter further being for production) require that the option be passed --enable-asserts so that the structures assert function. Thus, to compile the source code is made:

dart --enable-asserts <nome_do_arquivo_fonte>.dart

// ou

dart2js --enable-asserts <nome_do_arquivo_fonte>.dart [-o <nome_do_arquivo_resultante>.js]

If no option is given, the structures assert who have gifts will have no effect.

And as requested, here’s a screenshot use with and without the option:

Screenshot dos comandos com e sem a opção '--enable-asserts'

And also with the custom message:

Screenshot com saída customizada após falha no 'assert'

Using assertions in Flutter

And to use the assertions in Flutter, it is sufficient that the app is running in mode debug, which is usually the default mode during development.


I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.