Ad
Flutter - If Then Else - Variable Made Local Only
As you can see in the code below, I want to check if a string has a ? in his name. If yes, I remove it. My problem is that the variable 'nameOfFileOnlyCleaned' stay local and is empty after the if else. Thank you for your help.
String nameOfFile = list_attachments_Of_Reference[j].toString().split('/').last;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
String nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
String nameOfFileOnlyCleaned = nameOfFile;
}
//Here my variable 'nameOfFileOnlyCleaned' is empty.
This is a problem because the value should be used later
in the code. Do you know why I have this issue please?
Many thanks.
String extensionFile = nameOfFileOnlyCleaned.split('.').last;
String url_Of_File = list_attachments_Of_Reference[j].toString();
Ad
Answer
you should define your variable before if/else statement as follows:
String nameOfFileOnlyCleaned = "";
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
for example:
String nameOfFile = "test?test";
String nameOfFileOnlyCleaned;
if (nameOfFile.contains('?')) { //Removes everything after first '?'
nameOfFileOnlyCleaned = nameOfFile.substring(0, nameOfFile.indexOf('?'));
} else{
nameOfFileOnlyCleaned = nameOfFile;
}
print(nameOfFileOnlyCleaned);
it returns: test
as a result.
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad