AdearienSO
AdearienSO

Reputation: 111

A value of type 'String?' can't be assigned to a variable of type 'String'

What is wrong in here ? This is the 1st program i write in Flutter, and it failed. Please help me, this is a copy character for character from the tutorial

import 'dart:io';


void main() 
{
  stdout.writeln('Type in your name please');
  String  input = stdin.readLineSync();


  stdout.writeln('Hi $input nice to meet u ');
  
}

Upvotes: 9

Views: 26144

Answers (6)

JMax
JMax

Reputation: 797

Dart 3 has introduced "Null Safety". This means readLineSync returns 'String?', which accepts null values, but 'String' does not.
One way of fixing this is to use '.toString()'. This will return the string "null" instead of null.

main() {
  stdout.writeln('Type in your name please');
  String input = stdin.readLineSync().toString();
  stdout.writeln('Hi $input nice to meet u ');
}

Note that readLineSync() returns a blank string when you enter nothing, so you will only see "Hi nice to meet u" and not "Hi null nice to meet you"

This also works on a standard string...

String? aNullableString = null;
String aString = aNullableString.toString();

Upvotes: 3

Mr-Goonies
Mr-Goonies

Reputation: 1

The solution to your problem is as follows:

import 'dart:io';

main() {
  stdout.writeln('Ingresa tu nombre'); //Genera un prompt para el ingreso de datos
  var nameUser = stdin.readLineSync(); //Almacena el dato ingresado en el prompt en la variable 'nameUser'
  print('Tu nombre es: $nameUser');

}

Upvotes: 0

Mohammed Ouedrhiri
Mohammed Ouedrhiri

Reputation: 159

It's Just Because in Dart 3 there is a feature Added Called Null Safety which is used to guarantee that the input won't Be Null So you need just add a '!' in Your 'stdin.readLineSync()' and You are done Here is The Code :

    import 'dart:io';


    void main() 
    {
      stdout.writeln('Type in your name please');
      String  input = stdin.readLineSync()!;
    
    
      stdout.writeln('Hi $input nice to meet u ');
      

}

Upvotes: 10

Leon Presby
Leon Presby

Reputation: 31

You can also change String to String? like this:

import 'dart:io';

void main()
{
    stdout.writeln('Type in your name please');
    String? input = stdin.readLineSync();

    stdout.writeln('Hi $input nice to meet u ');
}

Upvotes: 3

This error is about "Null Safety", readLineSync returns 'String?', and your variable input does not accept null-values. This code can help you:

void main(List<String> args) {
  print('Type in your name please:');
  var input = stdin.readLineSync();

  print('Hi $input, nice to meet you');

  print('Done!');
}

Upvotes: 6

OctopuSS7
OctopuSS7

Reputation: 488

First off, that's dart, not flutter. Flutter is the framework. If you are learning just dart at the moment, InteliJ IDEA is the best dart ide. There is a community edition available for free.

Something like this will work:

import 'dart:io';

void main(){
  stdout.write("Type in your name please: ");
  var name = stdin.readLineSync();
  stdout.writeln('Hi $input nice to meet u ');
}

stdin.readLineSync(); doesn't actually return a String, so that you need to use var (infered typing). Use name.runtimeType; to find the type that it returns.

Upvotes: 0

Related Questions