I cannot validate the current computer time with IF ELSE, on Dart

Asked

Viewed 124 times

0

I’m trying to do a validation on Dart, where my function timevalider() has a variable of type dynamic, where he fits in with what I put in.

I’m doing the following assignment time = new DateTime.now().toIso8601String(), because with this I take the current time of my computer, which is returned, by exmeplo:

2020-04-03T16:05:29.620

I made a condition where returns me the text "Bomdia", case the time is less than and equal to 6.

But when I run it appears this message:

inserir a descrição da imagem aqui

How could I fix this mistake, I’m floating in something? There’s a way to do it in a different way, you could help me build it?

Follows the Code:

class Personagem{

  String nome;
  String povoado;
  int level;
  String classe;
  String habilidade;


  void levelskill(){
    level++;
  }

  //Só para assinantes
  void levelskillx4(){
    level +=4;
  }

  void renascer(){
    print("$nome morreu e renasceu no altar");
  }

  void death(){
    print("Você morreu! você perdeu uma porcentagem de level");
    level--;
    print("$level");
  }

   //Só para assinantes
  void premium(){
    print("Parabens $nome, agora você é premium, você tem 31 Dias.");
  }

  void timevalider(){
    dynamic time = (new DateTime.now().toIso8601String());
    if( time <= 6 || time <= 13){
      print("Morning");
    } else if (time >= 13 || time <19){
      print("Good Afternoon");
    } else print("Good Night");
  }
}

void main(){

Personagem pessoa1 = Personagem();

  pessoa1.nome = "testeper";
  pessoa1.povoado = "canada";
  pessoa1.level = 5;
  pessoa1.classe = "mago";
  pessoa1.habilidade = "Magia e Controle";

  print(pessoa1.nome);
  print(pessoa1.povoado);
  print(pessoa1.level);
  print(pessoa1.classe);
  print(pessoa1.habilidade);

  pessoa1.premium();

  pessoa1.timevalider();
  print(new DateTime.now().toIso8601String());
}

1 answer

1


You are using the method .toIso8601String() which will return the date and time at String, and soon after you are using that String to perform numerical operations with operators >=, <=, which makes no sense, so the mistake.

I suggest you modify your method timevalider() to the following:

void timeValidator(){
  final time = DateTime.now();
  if(time.hour <= 13) {
    print("Morning");
  } else if(time.hour > 13 && time.hour <19) {
    print("Good Afternoon");
  } else print("Good Night");
}

There’s no point in using Dynamic in this case, so let the guy DateTime, and with the property hour it is possible to catch the time. So your code will work.

Note that you did time.hour <= 6 || time <= 13), which is also unnecessary because if it is <= 13 it is <= 6.

  • It worked out I don’t even know how to thank, what would be this function "final" ??

  • But again thank you, now I understand my grotesque mistake, I’m enjoying the language as a whole, see excuse can I just ask you one more thing? it is possible to have input functions in Dart? type an equal language scanf c

Browser other questions tagged

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