Reputation: 329
I want to add conditional for image but it show an error on _setImage()
, how to fix it?
The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.
class _SplashScreenState extends State<SplashScreen> {
final String appName = AppConfig.appName;
String _setImage() {
if(appName.isNotEmpty == '') {
return 'assets/something1.png';
} else if(appName.isNotEmpty == '') {
return 'assets/something2.png';
}
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(_setImage()) //call your method here
),
),
);
}
}
Upvotes: 0
Views: 71
Reputation: 24950
The simplest of all the answer is try to add default return,
String _setImage() {
if(appName.isNotEmpty == '') {
return 'assets/something1.png';
} else if(appName.isNotEmpty == '') {
return 'assets/something2.png';
}
return 'assets/default.png'; 👈 This needs to be added
}
Because your return type is String
and you are returning String
only if appName.isNotEmpty == ''
in your first if and appName.isNotEmpty == ''
in your second if.
What if appName.isNotEmpty == ''
condition is not met? so you need to have a default return .
Your second else if is not necessary, change your code to :
String _setImage() {
if(appName.isNotEmpty == '') {
return 'assets/something1.png';
return 'assets/something2.png'; 👈 This is the best possible solution
}
Upvotes: 1
Reputation: 111
It is showing this error because your function's return type is String which is non-nullable.
You have't write the code for if neither of your conditions gets true then what to return.
Following are two solutions for it.
Try to make _setImage()'s return a nullable String. So function's return type should be String? Like following code snippet.
String? _setImage() {
if(appName.isNotEmpty == '') {
return 'assets/something1.png';
} else if(appName.isNotEmpty == '') {
return 'assets/something2.png';
}
}
OR
Return a String at every place where your function can possibly return.
String _setImage() {
if(appName.isNotEmpty == '') {
return 'assets/something1.png';
}
return 'assets/something3.png'; //Default image if neither of the conditions gets satisfied.
}
However, I recommend you to use this package another_flutter_splash_screen 1.1.4 as it is highly customizable for splash screens.
Upvotes: 0
Reputation: 321
appName.isNotEmpty is already a condition.
Change your code like this :
if(appName.isNotEmpty) {
return 'assets/something1.png';
}
return 'assets/something2.png';
Upvotes: 1
Reputation: 63
isNotEmpty is a getter and returns a boolean value that's why you can simply call it without adding further condition to it.
String _setImage() {
return appName != null && appName.isNotEmpty ? 'assets/something1.png' : 'assets/something2.png';
}
Upvotes: 1
Reputation: 101
It's because appName.isNotEmpty is already a condition.
To fix:
String _setImage() {
if(appName.isNotEmpty) {
return 'assets/something1.png';
} else {
return 'assets/something2.png';
}
}
Upvotes: 1