Method into classless files. Flutter

Asked

Viewed 39 times

2

Guys I came across a code inside a flutter file (Dart) that left me with some doubts, is a classless Dart file with only one method declared. I tried to look at the documentation but I guess I didn’t know how to search.
I’d like to know the possible prejudice of using this.

Dart file.

String getErrorString(String code){
  switch (code) {
    case 'ERROR_WEAK_PASSWORD':
      return 'Sua senha é muito fraca.';
    case 'ERROR_INVALID_EMAIL':
      return 'Seu e-mail é inválido.';
    case 'ERROR_EMAIL_ALREADY_IN_USE':
      return 'E-mail já está sendo utilizado em outra conta.';
    case 'ERROR_INVALID_CREDENTIAL':
      return 'Seu e-mail é inválido.';
    case 'ERROR_WRONG_PASSWORD':
      return 'Sua senha está incorreta.';
    case 'ERROR_USER_NOT_FOUND':
      return 'Não há usuário com este e-mail.';
    case 'ERROR_USER_DISABLED':
      return 'Este usuário foi desabilitado.';
    case 'ERROR_TOO_MANY_REQUESTS':
      return 'Muitas solicitações. Tente novamente mais tarde.';
    case 'ERROR_OPERATION_NOT_ALLOWED':
      return 'Operação não permitida.';
    default:
      return 'Um erro indefinido ocorreu.';
  }
}
  • Dart is an object-oriented language and in Dart functions are also object of language.

1 answer

2

There is no problem with this function and it is something common to exist. Functions need not be associated with classes, in fact, if a 'function' is within a class as you mentioned, it is actually called a method (object orientation concept).

The very function main() language is not associated with any class and is freely declared in a file. As well as the print(), among several others.

int minhaFuncao() => 42;

class MeuObjeto {
  int meuMetodo() => 42;
}

void main() {
  print(minhaFuncao()); // 42
  print(MeuObjeto().meuMetodo()); // 42
}

Dart functions are objects

The functions, like everything in Dart, are also objects of the type Function. As proof of this, just take any function and see that you can call methods and attributes that are present in the class Object:

int minhaFuncao() => 42;

void main() {
  print(minhaFuncao.toString()); // Closure: () => int from Function 'minhaFuncao': static.
  print(minhaFuncao.runtimeType); // () => int
}

Because of this, they can be assigned to variables or passed by parameter to other functions/methods:

void main() {
  final lista = [4, 2];
  lista.forEach(print); // 4 // 2
}

Notice how the function print is passed by parameter to the method forEach from the list. It can receive any function that has the required signature:

void forEach(void f(E element)) {
  for (E element in this) f(element);
}

Classes as functions

Another possibility is to have a certain class treated as a function. Calls Callable Classes. Just implement the method call() within it:

class Resposta {
  void call() => print(42);
}

final respostaSobreAVidaUniversoETudoMais = Resposta();

void main() {
  respostaSobreAVidaUniversoETudoMais(); // 42
}

Browser other questions tagged

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