Search code examples
flutterdartoperatorsoperator-keyword

What is "!" in Dart Flutter?


I know ! is used for NOT operation, but i find a code that use ! to fill variable with nullable type variable. When i read some code reference from internet i find this:

 NoteDetailPage(noteId: note.id!)

This is what it look like in NoteDetailPage:

const NoteDetailPage({
    Key? key,
    required this.noteId,
  }) : super(key: key);

note.id is a nullable int type and noteId is in int. If i go noteId: note.id then it will return conversion error error: The argument type 'int?' can't be assigned to the parameter type 'int'., but when i plug !, it doesn't show any error

i tried to do a google search, but it says that "!" is for NOT operations, but I don't think it makes any sense to put NOT operations in that code. Does anyone know the function of "!" on that code?


Solution

  • The name of the sign ! is Bang operator in Dart.

    By using ! operator, you are telling the compiler the variable won't be null.