skip to Main Content

I’m new to flutter development, at starting everything was well and good, but now everytime i run apy app i’m getting the following error. Please show me some ways to fix it.

C:/Users/Admin/AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.8.0/lib/palettes/tonal_palette.dart:164:30: Error: The argument type 'double?' can't be assigned to the parameter type 'double' because 'double?' is nullable and 'double' isn't.
        tone, () => Hct.from(_hue, chroma, tone.toDouble()).toInt());
                             ^
/C:/Users/Admin/AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.8.0/lib/palettes/tonal_palette.dart:164:36: Error: The argument type 'double?' can't be assigned to the parameter type 'double' because 'double?' is nullable and 'double' isn't.
        tone, () => Hct.from(_hue, chroma, tone.toDouble()).toInt());
                                   ^
/C:/Users/Admin/AppData/Local/Pub/Cache/hosted/pub.dev/collection-1.18.0/lib/src/canonicalized_map.dart:175:47: Error: Method 'call' cannot be called on 'bool Function(K)?' because it is potentially null.
Try calling using ?. instead.
      (_isValidKeyFn == null || _isValidKeyFn.call(key) ?? false);
                                              ^^^^
Failed to compile application.

I have tried ‘dart fix –apply’, also tried to do some modification mentioned in chatgpt, but nothing seems working for me. maybe i’m doing some mistakes.

2

Answers


  1. The reason is that nullable types and their non-nullable analogs are different types in dart types system.

    While converting nullable type to non-nullable you should do one of two things:

    1. If your sure you’re getting not-null value, you can apply non-null assertion operator:
    double? xOrNull = mayBeGetX();
    double x = xOrNull!; // Not-null assertion
    
    1. Otherwise you must explicitly check whether it is null and do something with this case (error, exception, log message, etc)

    Read more details here Dart: understanding null safety

    Login or Signup to reply.
  2. that’s because you don’t use null safety correctly.

    the meaning of double? is same as double or null and the methods toDouble and toInt cannot be used on null so this is the reason for the compile error.

    double? x = 0.0;
    // if you are sure that x is not null you put an ! sign after x and the result is not nullable
    int y = x!.toInt();
    // if you are not sure, you put ? after x and the result is nullable
    int? y = x?.toInt();
    

    for the second error I failed to understand your intensions

    (_isValidKeyFn == null || _isValidKeyFn.call(key) ?? false)
    

    if you want to return true when the function _isValidKeyFn is null but false when the function is not null but have return type of null
    then the right expression is: (notice the ! after _isValidKeyFn)

    (_isValidKeyFn == null || _isValidKeyFn!.call(key) ?? false)
    

    but if your intension is to return false even if the function is actually null, then the right expression is:

    _isValidKeyFn?.call(key) ?? false
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search