Search code examples
flutterdartdart-null-safety

File path error when creating model (String error)


The following error message occurred. "The argument type 'String?' can't be assigned to the parameter type 'String’"

[enter image description here]

img

I don't understand why this is an error. If there is anyone who knows the solution, please let me know.

i want to make model and solve that... thank you...


Solution

  • The value imageFilePath could be null so that means:

    final face = Face(name:faceName,imagePath:imageFilePath!);
    

    or

    You could make the field imageFile in your Face class null-aware:

    class Face {
    
    final String name;
    final String? imagePath;
    
    Face({required this.name,required this.imagePath});
    
    }
    

    then you can easily do this:

    final face = Face(name:faceName,imagePath:imageFilePath);

    There is another option if you don't want to do the above two suggestions:

    final face = Face(name:faceName,imagePath:imageFilePath ?? '');