Resolved: How to fix nullable error in flutter Uint8List

In this post, we will see how to resolve How to fix nullable error in flutter Uint8List

Question:

I am quite new to flutter and challenging to create to show images in grid downloading file saved in firebase storage.
Getting error code as below and I would like to know how I can fix nullable part…
lib/artfolder.dart:53:21: Error: A value of type ‘Uint8List?’ can’t be assigned to a variable of type ‘Uint8List’ because ‘Uint8List?’ is nullable and ‘Uint8List’ isn’t.
  • ‘Uint8List’ is from ‘dart:typed_data’. imageFile = data; ^ lib/artfolder.dart:47:14: Error: Field ‘imageFile’ should be initialized because its type ‘Uint8List’ doesn’t allow null.
  • ‘Uint8List’ is from ‘dart:typed_data’. Uint8List imageFile; ^^^^^^^^^

Here is my code;;

Best Answer:

You just need to declare it as a nullable and check if its null.
Step 1:
Step 2:
Step 3:

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com