Error: The argument type 'String? ' can’t be Assigned to the Parameter type 'String' because 'String? ' is nullable and 'String' isn’t

Asked

Viewed 1,817 times

-1

In the code I’m doing, is being mounted an if to know if a certain age is greater or smaller and show the messages, so far so good.

The point is, when I use the integer value, I can work normally, but when I enable the input to inform the value in the input, it shows me this message:

Error: The argument type 'String? ' can’t be Assigned to the Parameter type 'String' because 'String? ' is nullable and 'String' isn’t..

I know this giving a string to integer conversion error, but I’ve looked for other ways, using int.tryParse, int.parse (which is what shows in the course and the video works), inform the value already with int, but none of the ones I tried works.

I would like some help if you could just let me know what I’m missing or what else I could read to get an idea of why it’s not working.

Code:

import 'dart:io';

main() {
  print("==== Digite uma idade ====");

  var input = stdin.readLineSync();
  var idade = int.parse(input);

  if (idade >= 18) {
    print("Maior de Idade");
  } else {
    print("Menor de Idade");
  }
}

Thank you in advance.

  • You tried to create int variables instead of var?

2 answers

1

Your problem is that you are using null-Safety and are trying to pass a value String? for a parameter that asks only String...

Like the guy var "becomes" the type of value you report, it becomes a String? for the readLineSync returns this type.

import 'dart:io';

main() {
  print("==== Digite uma idade ====");

  String input = stdin.readLineSync() ?? "";
  int idade = int.parse(input);

  if (idade >= 18) {
    print("Maior de Idade");
  } else {
    print("Menor de Idade");
  }
}

Explanation

Always try to type your variables for the type they should receive, so as not to cause confusion later.

The change comes down to:

• Type variable to String, saying that it will not be able to receive the null value.

String: Says the variable will always have a string. String?: Says the variable can receive text or null.

• Testing whether the readLineSync() returned a null value, if positive, returns an empty text.

stdin.readLineSync() ?? ""

Note: This way solves the main problem, but if it returns empty "", can give problem in parse, then you treat as you see fit.

0


Opa Maurilio, I am in the same lesson as you; here goes my solution:

import 'dart:io';

void main(){

  // Verificar se a pessoa é maior de idade
  // Se a idade for maior ou igual a 18
    
var textidade = stdin.readLineSync();  // Essa linha adquire a variavel

  if (textidade != null) {  // essa linha testa se o valor é diferente de nulo
    int? idade = int.tryParse(textidade); // aqui surge a variavel int(numero inteiro) e o tryParse "pega"
    if (idade != null) { //aqui ele testa novamente se o inteiro é diferente de nulo
    
    // a partir daqui vc coloca o codigo da aula
    
      if(idade >= 18){
        print("Maior de idade");
      } else {
        print("de Menor");
      }
    }
  }
}
  • 1

    Uides, good afternoon. Thank you for your help. Both forms worked and I used another way and also worked, but both mine and the first comment give an error when the value is null and in your case it makes the check.

Browser other questions tagged

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