3

Hi Im following one of the yt tutorials and when I try to make a model this error occurs what is my best approach to solve this problem. I cannot put required because some values do take null. My project is based I'm taking global API I found on net similar to http://www.omdbapi.com/ (Im not using this but this is just a point of refference) and when I do so I get this error:

    The parameter 'title' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
{String title}

This is my books class:

class Book{
      String name;
      String title;
      String city;
      Book(
          {this.name,
            this.name,
            this.name});
      factory Book.fromJson(Map<String,dynamic> json){
        return Book(
          name: json['name'] as String,
          title: json['title'] as String,
          name: json['name'] as String,
        );
      }
    
    }

What is the best solution to this problem because when they do it this way they have no error at all. Looking forward to your answers

Thanks in advance

Vedo
  • 976
  • 4
  • 21
  • Does this answer your question? [The parameter can't have a value of 'null' because of its type in Dart](https://stackoverflow.com/questions/64560461/the-parameter-cant-have-a-value-of-null-because-of-its-type-in-dart) – Kirill Bubochkin Jan 29 '22 at 14:01
  • Take a look at my answer, I´d suggest you Option 3 as you said they can be null - so make your values nullable! Also, you have made a mistake with your constructor: You are using this.name three times, this of course needs to reference all three parameters (name, title and city)! :) – Jahn E. Jan 29 '22 at 14:06

3 Answers3

3

You are using named parameters and in your case they can be null. You need to add required to the parameters in the constructor:

class Book{
      String name;
      String title;
      String city;
      Book({
          required this.name,
          required this.name,
          required this.name
      });
      
    factory Book.fromJson(Map<String,dynamic> json){
        return Book(
          name: json['name'] as String,
          title: json['title'] as String,
          name: json['name'] as String,
        );
      }
    
    }

Or if the parameters can be null:

class Book{
      String? name;
      String? title;
      String? city;
      Book({
          this.name,
          this.name,
          this.name
      });
      
    factory Book.fromJson(Map<String,dynamic> json){
        return Book(
          name: json['name'] as String?,
          title: json['title'] as String?,
          name: json['name'] as String?,
        );
      }
    
    }
st3ffb3
  • 398
  • 3
  • 15
1

As the error message suggests you can either:

Option 1: Add the 'required' modifier because the default_value_for_parameter is missing:

  Book({
          required this.name,
          required this.title,
          required this.city
      });

Option 2: Try adding either an explicit non-'null' default value

 String title = ""; // Empty string as a default value

Option 3: Make the value nullable (Be careful with this, only do it if the value should be nullable)

String? title;

As you mentioned, that they can be null, your code needs to reflect that as well. Therefore, you have to make your parameters nullable by adding the "?" after the type annotation. Just as a reminder, use null checks whereever you need a non-nullable value ( if(value != null) > then do something ) to avoid run-time null errors :)

Jahn E.
  • 1,228
  • 3
  • 6
  • 21
1

Ok so the solution for my problems was quite strange indeed. All I had to do is whitch from sdk: ">=2.15.0 <3.0.0" to sdk: ">=2.7.0 <3.0.0"

Vedo
  • 976
  • 4
  • 21
  • Well that is not a solution to be honest. You simply used a version constraint without null safety. Null safety is a huge improvement to dart, I deeply suggest you familiarize yourself with it, as there are many great articles out there. – Jahn E. Jan 29 '22 at 15:43