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:
And also with the custom message:
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!
The option
--enable-asserts
must be passed along with the file that will be executed, to activate theAssertionError
s. Already tried to ask them to offer a way to activate this option by Dartpad.– Gustavo Sampaio
That is, depending on the tool also being used to run the program, as the documentation says.
– Gustavo Sampaio
@Gustavosampaio If possible put as an answer exemplifying the use in the Dart console, would be even better with print of the result.
– rubStackOverflow
Okay, I’ll work out the answer.
– Gustavo Sampaio