I am having too many yellow lines in my flutter app - android

I need to write error-free, clean code for my flutter app.
In a stateless widget, if I delete const from the constructor, a yellow line error appears as seen below:
This class (or a class that this class inherits from) is marked as '#immutable', but one or more of its instance fields aren't final: PerytonDropDown.selected
I don't know how to deal with it. Can anyone help me to resolve this issue?

it is very simple, just initialize your variable with final.
Like Below :
final String text;

Related

Class name is marked as 'not covered' while running code coverage in Android Studio

I have tried AndroidStudio's code coverage feature and I have met a strange issue:
It is marks the tested class's name as 'not covered' code.
How is that possible? Is that a bug?
Picture here:
As you can see it has one method with 4 lines and each one of them is covered. So why is the red line at the class's name?
You are using a static method, so the class itself is never created as an object, therefore never testing that ability.
I tried the lombok #UtilityClass, it helped to ignore the class name and code coverage was improved to be 100%.
Since also a class with a static function has a default no-argument constructor, your code coverage tool will complain. A good way to solve this, is by adding a private constructor.
private EmailValidator() {
}

Android Studio cannot resolve symbol

I'm just starting with programming Android apps and was working through the developers guide on android.com. When trying to display a text on a second activity it says it cannot resolve the symbol EXTRA_MESSAGE as you can see here:
As far as I can tell I did every step like the guide says. I also tried copy and pasting everything but it still doesn't work. What am I missing?
You're doing a static import of extra message from some random class in MainActivity. That's wrong, don't do that. Define EXTRA_MESSAGE as a public final static String, with whatever value you want (I'd suggest "message") in your MainActivity.
There is two ways of solving it.
1) Use same static variable (Its a dirty way). At DisplayMessageActivity use android.provider.AlarmClock.EXTRA_MESSAGE.
2) This approach I recommend you. Create public static final String field at MainActivity, remove android.provider.AlarmClock.EXTRA_MESSAGE, and use MainAcitivity field at both classes. Content of this variable does not matter as long as it is unique extra key.

Variables with underline

I'm getting an underline in some variables on Android Studio (in this case on the 'position' variable). I think it's not an error because the application runs perfectly and the compiler passes everything ok.
I'm wondering what does that mean?
It could be a sign of "Reassigned parameter"
I believe the underlined variables are representative of constants (final or effectively final), because in my experience I only see this decoration when I declare a final object for use inside an anonymous class. I can't seem to find it in the documentation, though.
I've found the answer for this question here.
The decoration is a syntax highlighting preference. Take a look at File > Settings > Editor > Color Scheme > Java/Kotlin
In the case of Java, you can find this effect for example at Parameters > Implicit anonymous class parameter. It's the checkbox Effects.
The same with Kotlin at Properties and Variables > Var (mutable variable, parameter or property).
This means the variable was declared outside the current method. For example, in this case, position is probably declared as a class member outside the new DialogInterface.OnClickListener(), in the class where you're implementing the onItemLongClick() method.
They are declared like this:
public class MyClass{
private int position;
// Other code...
}
It may be because a immutable variable is subjected to modification. Like reassigning a String or trying to modify a final declared variable.
String buffer = "";
buffer = buffer + "new string";
Will underline the buffer, since string are of immutable Objects.
If you know what is the side effect in programming then it will be easy for you. To protect your variable from the side effect, the IDE shows the underline as a warning to you. Which is sometimes very helpful to reduce logical bugs in your code.

What is the meaning of putting class name inside the brackets?

While observing an existing android application, I encountered this line of code:
((StoredVariables)this.getApplication()).getId
Why we are using this? What is the outcome of the code? Does it returns previously stored values from sharedPreference? No documentation found on internet to get an idea about it. Please explain.
You are probably a little newbie in Java also
I'm not sure about what is "this" in this.getApplication() and what the StoredVariable is, since it is not an android class...
But here are recommendations for you:
Look for StoredVariable in your class in imports section. You can find something like import my.project.StoredVariable;
Open the class (I'm 80% sure it will be interface) to see the class (this is already answer to your question)
See getId() method there
To know more about usage of StoredVariables. If this line of code is placed inside class that extends some Activity (e.g. AppCompatActivity, Activity e.t.c.)
Open AndroidManifest.xml and look into <application> tag to find android:name in there. Open class with that name (ctrl + click or cmd + click on class name)
This class is extending Application class (or subclass) and implementing StoredVariables interface, or it is directly StoredVariable class

Settings variable in Android

Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.
A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";
You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.
I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.

Categories

Resources