'Assignment of null to File variable in Flutter
How can I assign null to File variable. I got error when assigning null to File variable.
File myImage=null;
(myImage==null)?
CircleAvatar(
backgroundImage:AssetImage('assets/user_new.png'),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
:
CircleAvatar(
backgroundImage:FileImage(myImage),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
Solution 1:[1]
I suppose you have nullsafety enabled. With nullsafety a variable declared without a ? cannot be null. To fix your issue do the following.
File? myImage = null;
Solution 2:[2]
In Flutter null safety is a thing.
With it, no variables can be null if they haven't got a ?. Add a question mark to your File declaration, as:
File? myImage = null;
Down in your code, you will need to check if myImage is null (since now it can be) if you want to assign it to the backgroundImage parameter. You have 2 choices for this:
backgroundImage:FileImage(myImage!) //Null check: Exception throw only if null
backgroundImage:FileImage(myImage ?? _anotherWorkingFile) //If: use another standard file if null
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | quoci |
| Solution 2 |
