Flutter - The argument type 'String?' can't be assigned to the parameter type 'String'

1,761

Type String? means it has to have a string or null in variable of this type. In your case, probably, you'd always need a title and this variable should not be nullable. So the solution is to change type to String and add required keyword to named parameter in constructor.

class OnbordingSliderTile extends StatelessWidget {

final String title;

  OnbordingSliderTile({required this.title});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(title)
    );
  }
}

I did used toString() method also but not sure whether it is the genuine way

if you use a toString() on String? it'll return value, if there is any, or simply "null" string, if it's null.

The error you got is caused by Text widget. It awares you that you were displaying as text something that can potentially contain null.

UPD: and please don't follow this advise:

use Text(title!) instead of Text(title) – Mehran Ullah

this is a very bad practice, destroying the whole point of null safety. Doing so means you insist there is not a null value within a nullable type variable, which can be avoided in most cases and is not safe as you might cause null error if you wouldn't have provided a value to your variable, which can happen as named parameter you were using was not required and didn't have any initial value, so you could simply not pass anything to your widget in constructor.

UPD

Is this the recommended way? Because what if I do not want the variable to be required? – user4258194

Text widget needs a String. It's a positional parameter, which means it's required. You can't have a Text widget without a text.

What are your options to provide it in your example widget?

  • a required parameter as shown in the initial part of the answer;

  • an optional non-nullable parameter with initial value:

     class OnbordingSliderTile extends StatelessWidget {
    
     final String title;
    
         //'Placeholder text' will be in title variable if you 
         // haven't provided a value for it when initializing OnbordingSliderTile
     OnbordingSliderTile({this.title = 'Placeholder text'});
     @override
     Widget build(BuildContext context) {
     return Container(
       child: Text(title)
       );
      }
     }
    
  • a nullable parameter with no initial value as you had it and a null check when using it in Text widget:

     class OnbordingSliderTile extends StatelessWidget {
    
     final String? title;
    
         //you can skip providing this parameter to the constructor and then it will be null
    
     OnbordingSliderTile({this.title});
     @override
     Widget build(BuildContext context) {
     return Container(
     // You have to check if value in your variable is a String, not null
     // and make sure you provided some text for the case if it null
        child: Text(title ?? 'Placeholder text')
       );
      }
     }
    

If you need this text to be empty when no value was provided, use empty String as Placeholder text in any of provided options.

Share:
1,761
user4258194
Author by

user4258194

Updated on December 30, 2022

Comments

  • user4258194
    user4258194 over 1 year

    After Flutter 2.0 null safety were introduced, but when it comes to getting the String variable Flutter is showing the error -

    The argument type 'String?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)

    I want to have a constructor, How to implement constructor with Null Safety? Also how to get the String variable.

    I did used toString() method also but not sure whether it is the genuine way.

    class OnbordingSliderTile extends StatelessWidget {
    
      final String? title;
    
      OnbordingSliderTile({this.title});
    
      @override
      Widget build(BuildContext context) {
        return Container(
          //This Text widget is showing the error
          child: Text(title)
        );
      }
    }
    
    • Mehran Ullah
      Mehran Ullah almost 3 years
      use Text(title!) instead of Text(title)
  • user4258194
    user4258194 almost 3 years
    Is this the recommended way? Because what if I do not want the variable to be required?