I am confused by how dart handles its class variables.
Consider the below snippet.
double income = 0.0;
printer() {
print(this.income);
print(income);
}
}
void main() {
Testing testing = Testing();
testing.printer();
}
Can someone enlighten me please.
Actually both prints 0. This is to refer to the class variable, if there is a name conflict.
Use this only when there is a name conflict. Otherwise, Dart style omits the this.
Related
I am trying to know which class has called a specific function. I've been looking through the docs for this, but without success. I already know how to get the name of a class, but that is something different of what I'm looking for. I found already something related for java but for dart I haven't. Maybe I'm missing something.
Let's say for example that I have a print function like so:
class A {
void printSomethingAndTellWhereYouDidIt() {
// Here I would also include the class where this function is
// being called. For instance:
print('you called the function at: ...');
//This dot-dot-dot is where maybe should go what I'm looking for.
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
The output should be something like:
you called the function at: B
Please let me know if there are ways to achieve this. The idea behind is to then use this with a logger, for instance the logging package. Thank you in advance.
You can use StackTrace.current to obtain a stack trace at any time, which is the object that's printed when an exception occurs. This contains the line numbers of the chain of invocations leading up to the call, which should provide the information you need.
class A {
void printSomethingAndTellWhereYouDidIt() {
print(StackTrace.current);
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
If you are doing this for debugging purposes, you can also set a breakpoint in printSomethingAndTellWhereYouDidIt to check where it was called from.
i'm coming from mainly JS/TS world (NestJS/Angular) and recently i start to building Flutter apps..
i have 2 main questions
there is any difference when instantiate object with or without new keyword?
i saw examples in flutter when people use new Row(children: [Text('Foo'), Text('Bar'),],) instead of just Row(...)
if there is a difference which one is better to use?
inside of my Dart classes in flutter app, i can both use this.property and property again there is any difference and if so which one is better and why?
example:
class Person {
final String name;
final int age;
Person(this.name, this.age);
getNameAge() => '${this.name} is ${this.age}';
getNameAge2() => '$name is $age';
}
both looks the same to me
void main() {
final p = Person('dan', 22);
final p2 = new Person('ben', 20);
print(p.getNameAge()); // dan is 22
print(p2.getNameAge2()); // ben is 20
}
The new keyword is optional in Dart and I think the general consensus is, today, to not use it.
The use of this is useful if you have multiple variables with the same name but in different scope. E.g. (this is just an example. You would not make a setA method in Dart but use properties):
class A {
int a;
A(this.a);
void setA(int a) {
this.a = a;
}
}
Here we use this to distinguish between the argument a and the class variable a. But if you don't have variables with the same name (but in different scope), the use of this is optional. In some projects, you still use this to make it more clear that you are referring to a class variable even if it is not needed.
I'm sorry if this sounds like an extremely foolish question but it's really been bugging me.
What is the "this." that I see? Whenever I see the documentation in flutter I see it used in things like the following in the documentation:
this.initialRoute,
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.navigatorObservers
I'll be more than happy to also read up any links or documentation regarding it.
The 'this' keyword refers to the current instance.
You only need to use this when there is a name conflict. Otherwise, Dart style omits the this.
class Car {
String engine;
void newEngine({String engine}) {
if (engine!= null) {
this.engine= engine;
}
}
}
So you can be consistent with the name of your parameters, either in the constructor or in some function in the class.
class Car {
String engine;
void updateEngine({String someWeirdName}) {
engine = someWeirdName;
}
}
If you don't have a name conflict, you don't need to use this.
In other languages like Python and Swift, the word 'self' will do the same thing as 'this'.
Basically, this keyword is used to denotes the current instance. Check out the below example.
void main() {
Person mike = Person(21);
print(mike.height);
}
class Person {
double height;
Person(double height) {
height = height;
}
}
When we run this dart code, it outputs null as the height. Because we have used height = height inside the Person constructor, but the code doesn't know which height is the class property.
Therefore, we can use this keyword to denotes the current instance and it will help the code to understand which height belongs to the class. So, we can use it as below and we will get the correct output.
void main() {
Person mike = Person(21);
print(mike.height);
}
class Person {
double height;
Person(double height) {
this.height = height;
}
}
Use of this keyword
The this keyword is used to point the current class object.
It can be used to refer to the present class variables.
We can instantiate or invoke the current class constructor using this keyword.
We can pass this keyword as a parameter in the constructor call.
We can pass this keyword as a parameter in the method call.
It removes the ambiguity or naming conflict in the constructor or method of our instance/object.
It can be used to return the current class instance.
Is it a good practice to use always 'this' in a method.
Even if there is no usage of the same variable name as input to this method.
String getStory(number) {
return this.storyData[number];
}
By doing this there won't have any conflicts with any input variable name (never).
Thank you in advance for your feedback.
Generally this. is only supposed to be used when there is ambiguity about what variable is being used.
class Test{
final int x;
Test(this.x);
}
I'm trying to customize the quick create view to add a default value of a field in Sugar Community Edition 6.5.24
Similar code works fine for ViewEdit, but it seems never called in subpanels.
Current file is
custom/modules/Opportunities/views/view.quickcreate.php
Unfortunately the constructor is not invoked.
Any help very appreciated.
<?php
require_once('include/MVC/View/views/view.quickcreate.php');
class OpportunitiesViewQuickcreate extends ViewQuickcreate {
function OpportunitiesViewQuickcreate(){
parent::ViewQuickcreate();
}
function preDisplay() {
parent::preDisplay();
$_REQUEST['custom_field_c'] = "a value for this field";
}
}
After tens of trying, I've found solution.
The right way is to extend SubpanelQuickCreate in the file custom/modules/Opportunities/views/view.subpanelquickcreate
require_once('include/EditView/SubpanelQuickCreate.php');
class OpportunitiesSubpanelQuickcreate extends SubpanelQuickCreate {
function OpportunitiesSubpanelQuickcreate() {
$_REQUEST['custom_field_c'] = "a value for this field";
parent::SubpanelQuickCreate("Opportunities");
}
}
Going from memory, so I may be wrong, but try adding $this->useForSubpanel = true; in your constructor.