Home > Software design >  Convert nullable variable with int.parse using dart
Convert nullable variable with int.parse using dart

Time:08-19

Do I need to convert a string? to an int if the value in the string is not null. (My routeArgs variable may be is null) I was doing according to this code before the null safety:

final routeArgs = ModalRoute.of(context)!.settings.arguments as Map<String, String>;
if(routeArgs["id"] != null){
   id = int.parse(routeArgs["id"]);
 }

But now it displays the error stating: "The argument type 'String?' can't be assigned to the parameter type 'String'." I tried to correct as the code below:

    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map<String, String>;
   
    int id;
    String? idtest = routeArgs["id"]; 
    if (idtest != null){
      id = int.parse(idtest);
    }

This code stopped giving error, but I think this is not the correct way to solve it, I would need to do this for several variable values. Would it be correct for me to make a function just for conversion? Could anyone give me an idea of a solution?

CodePudding user response:

Performing type checking is the best way, this also goes for modalroute args.

final routeArgs = ModalRoute.of(context)!.settings.arguments as Map<String, String>;

int id;
var idtest = routeArgs["id"]; 
if (idtest is String){
  id = int.parse(idtest);
}

Check args ModalRoute:

final routeArgs = ModalRoute.of(context)!.settings.arguments
if(routeArgs is Map<String, String>){
  var id = 0;
  var idtest = routeArgs["id"]; 
  if (idtest is String){
    id = int.parse(idtest);
  }
}

CodePudding user response:

final routeArgs = ModalRoute.of(context)!.settings.arguments as Map<String, String>;

int id;
String? idtest = routeArgs["id"];
if (idtest != null){
  id = int.parse(idtest);
}

The above isn't quite right because id is not declared to be nullable nor is it declared to be late, and since not all code paths initialize id, you will get a compilation error if you attempt to actually use id.

Aside from that, adding local variables to allow automatic type-promotion to occur is usually the right thing to do. However, in this case, depending on exactly what you want, you perhaps could be more succinct if you don't mind being slightly less efficient:

int? id = int.tryParse(routeArgs['id'] ?? '');

or if you want id to have some non-null default value:

int id = int.parse(routeArgs['id'] ?? '0');

Note that the first version has different behavior from your original code: if routeArgs['id'] is an unparseable String, null will be assigned to id instead of throwing a FormatException.

CodePudding user response:

If you are shure routeArgs['id'] returns an string, you can do

final String idTest = routeArgs['id'];

Remember, with null-safety, if you are sure that some nullable value will not be null on a specific runtime, you can tell the compiler that with !.

For example

User? user; //Here the user variable is null
user = User(); 
user.id; // if you do this, the compiler will throw an error, because you want the id of the user, but the variable user could be null. 
Here you have two options
  1. user?.id ?? 'guid' // You provide a "default" value if the user variable is null

  2. user!.id // Here you ensure to the compiler that at this point, the variable user will **NOT** be null

  • Related