3

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();

3
  • because your String is inside the if/else statement and you should have the variable on a upper scope. I recommend to read about general Object Oriented Programming principals to understand scope. Commented Jul 30, 2021 at 14:27
  • Thank you. I have declared those variables before the class. So I thought that it could be available every where in the class. But I do not know how to fix this. Commented Jul 30, 2021 at 14:31
  • 1
    They need to be inside the class but outside the if/else statement. You have to declare them as "late" or optional (using ?) Commented Jul 30, 2021 at 15:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I have tried, but my variable nameOfFileOnlyCleaned remain empty. I still not understanding why.
I just checked the result, it works fine, for example you can check the example (I have updated my answer). Please check the value of list_attachments_Of_Reference[j].toString().split('/').last;
Fantastic. It works. Many thanks for your help. It is appreciated.
Could you give a reason, why it needs to be defined like this?
@Uwe.Schneider search for variable scope in if-statements in dart. Defining a variable inside if-statement is specifically for that scope in some programming languages such as C, Dart, etc. But for example in Python, you wont have this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.