Can't assign non-nullable type to a nullable one

1.1k Views Asked by At
error: The argument type 'Future<List<GalleryPictureInfo>>' can't be assigned to the parameter type 'Future<List<GalleryPictureInfo>>?'.

Is this Dart Analysis or me? The project still compiles.

Upd. Added code example

FutureBuilder<List<GalleryPictureInfo>>(
  future: derpiService.getListOfImages(),
  //other code
);
    
@override
Future<List<GalleryPictureInfo>> getListOfImages(arguments) async {
  List<GalleryPictureInfo> listOfImages = [];
  var searchImages = await getSearchImages(tags: tags, page: page);
  //adding images to List
  return listOfImages;
}

It's something with FutureBuilder actually. I should've mention this.

Upd. "Fixed" with // ignore: argument_type_not_assignable

Looks like a problem with Dart Analysis for now

Upd. Error

1

There are 1 best solutions below

1
Just a Person On

It actually is an error which is pretty self explanatory. The acutal error comes because of null safety in dart. For ex:

void main(){
     var number = getNumber(true);
     int parsedNumber = int.parse(number);
     print(parsedNumber);
}

String? getNumber(boolean value) {
     if (value){
          return null;
     } else return "1";
}

So here, getNumber function either returns null or "1" depending upon the value of value variable. So, number variable's type is String?. But the error shall arise in the next line when you try to call int.parse(). int.parse function takes an argument which should be a String but the value passed in the function is of type String?. So if we pass null in int.parse it shall throw an error. That's why Dart analysis makes it easier to identify such cases by telling us that the value can be null and it might throw.

However the code depends upon your actual code of your project. It says that you are passing Future<List<GalleryPictureInfo>>? which is of nullable type to a function which requires Future<List<GalleryPictureInfo>>. So, before passing the value you might want to check if the value you are passing is not null.

If you are sure that the value can never be null then if for ex: if you are passing a variable called value, you might wanna try someFunctionWhereYouPassValue(value!)

That ! means that you are sure that the value will never be null.

For more details about null safety you can see: https://dart.dev/null-safety/understanding-null-safety