Home > front end >  The argument type 'String?' can't be assigned to the parameter type 'String'
The argument type 'String?' can't be assigned to the parameter type 'String'

Time:02-25

I have the following code:

var _editedService = Service(
  id: '',
  name: '',
  description: '',
  aboutUs: '',
  streetNumber: null,
  streetName: '',
  suburb: '',
  city: '',
  province: '',
  zipCode: null,
  contactNum: null,
  email: '',
  logoUrl: '',
  imageUrl: '',
);

When I run this I receive the following error:

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.

I've went through the other Stack response but there were no success.

Any advice?

This is the Service Class:

class Service with ChangeNotifier {
  final String id;
  final String name;
  final String description;
  final String aboutUs;
  final double? streetNumber;
  final String streetName;
  final String suburb;
  final String city;
  final String province;
  final double? zipCode;
  final double? contactNum;
  final String email;
  final String logoUrl;
  final String imageUrl;
  late final bool? isFavorite;

  Service({
    required this.id,
    required this.name,
    required this.description,
    required this.aboutUs,
    required this.streetNumber,
    required this.streetName,
    required this.suburb,
    required this.city,
    required this.province,
    required this.zipCode,
    required this.contactNum,
    required this.email,
    required this.logoUrl,
    required this.imageUrl,
    this.isFavorite = false,
  });

  void toggleFavoriteStatus() {
    isFavorite = !isFavorite!;
    notifyListeners();
  }
}

CodePudding user response:

I tested the code in dartpad and it's totally fine, there is nothing wrong with it.
So some other part of the code is throwing the error, update your answer with a detailed error message like error line number etc. The problem is related to dart null safety.

Here is an example:

main() {
  print(add('hello', 'world'));
}

String add(String a, String? b) {
  return a   b;
}

The above block of code will raise an error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't

And to solve it, you have to use the null-aware operator:

main() {
  print(add('hello', 'world'));
}

String add(String a, String? b) {
  return a   b!;
}

Note: the !(exclemation mark) after b in return statement. It will solve the problem.

CodePudding user response:

use "!" at the end of what you are returning

String? xya = "safdd";
String yz = xya!;

CodePudding user response:

This may help you

With null safety on, variables cannot be null if they haven't got a ? at the end. For example:

String myString = "Hello World"; //This CANNOT be null
String? myString = null; //This CAN be null

The solution may be putting a null check ! as or replace with an empty string if null.

String myString = myOtherString!
String myString = myOtherString ?? "Something else";
  • Related