0
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

Selqet
  • 1
  • 1
  • Please include the codes – Kaushik Chandru Aug 21 '22 at 13:47
  • Are you reassigning the variable `listOfImages` anywhere in the function? – Just a Person Aug 21 '22 at 15:06
  • No, it returns "as is" straight to the FutureBuilder – Selqet Aug 21 '22 at 15:42
  • Did you copy and paste the exact error message? That error shouldn't be possible. An instance of `T` should ways be assignable to a `T?`. Do you possibly have multiple classes named `GalleryPictureInfo` (or are you possibly importing the library that defines `GalleryPictureInfo` with inconsistent package paths)? – jamesdlin Aug 21 '22 at 16:29
  • Yes, it's exact the same message, that's the problem. I don't have multiple classes with that name. Actually it's just a DTO without methods. – Selqet Aug 22 '22 at 06:59
  • Can you provide a complete example that reproduces the problem? – jamesdlin Aug 25 '22 at 19:18
  • Had the same issue and the import statement was import 'MyClass.dart'; instead of import 'myClass.dart'; Fixing the casing solved it. – Chris Herring Apr 28 '23 at 13:05

1 Answers1

0

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

Just a Person
  • 1,276
  • 1
  • 5
  • 23