What is the point of adding 'new' in front of a widget in Flutter? [duplicate] - flutter

This question already has answers here:
Do you need to use the "new" keyword in Dart?
(3 answers)
Closed 2 years ago.
For instance,
return new Container(child: new Text('Hello'),);
VS
return Container(child: Text('Hello'),);
I see people use both and was just wondering what the point is. Is it possible to use the same widget again somewhere or something?

the "new" keyword was once required in dart (the language used for flutter) for object instantiation, but this requirement was removed.
In order to prevent breaking old code, the keyword remains but is now optional. There is no difference between using it or not using it.
You may choose to use it if you want to make it clear that you are intending to do object instantiation and not call a function, as attempting to do new somefunction() //not a constructor will fail, though this is optional.

Related

Is specifying list items type important in Dart?

I recently started learning Flutter and hence Dart.
Unlike Python, everyone seems to be specifying the datatype of the list items whenever they create a list in Dart. I don't really want to, so is it really important to do so, or is it just one of those norms?
List<String> names = [
"Faisal",
"Saifi"
];
It still works without doing it so let me know should I or should I not. As there isn't really this question anywhere asked here (Or I didn't come across it).
You don't need to specify the type of list when you are defining it, but in a complex app in order to avoid facing assigning value error, it is good to define list with type.
for example you supposed to getting List of string from api bit some you are getting list of int if you define you list like List, you never notice that and you will get error some where else in you complex app, but if you define your list like List<String> you will get exception on that and you now why that is happening.

Dart - How to access global scope? [duplicate]

This question already has answers here:
How can I call a function from a method of the same name?
(3 answers)
Closed 12 months ago.
The simple actual question is
How to access GLOBAL scope in Dart?
Below is just the answer why I'm looking for it.
I have a problem which requires me to learn about Dart's scope resolution from here but unfortunately I can't find the solution for my problem, not even from Google or here, which was surprising me to have to open this question.
Dart allows user to exclude the this. on member variable access within class's scope so if two potentially conflicted variables need to share the same name, they need to be differentiated with scope operator and reference.
I need to define a class with member variable print where there is also global function print being called somewhere within the class. Of course print() => print(...) will returns error so I'm expecting something as simple as print() => global.print(..) but neither that is working.
I know there's workaround to solve it but please I need a straightforward one if any. Thank you.
you could add this line at the top of your file:
import 'dart:core' as core;
Then you would be able to refer to print as core.print(...);
The problem is that now you would have to use core. for every primitives like int, example:
core.int variable = 1;
I think this is not worth it and it's better to use another name for your print method.

What does external keyword mean in Dart? [duplicate]

This question already has answers here:
What does external mean in Dart?
(6 answers)
Closed 2 years ago.
For example: this is a private named constructor of DateTime class from date_time library :
external DateTime._now();
I found a stack overflow post asking the same question:
What does external mean in Dart?
9.4 External Functions An external function is a function whose body is provided separately from its declaration. An external function may
be a top-level function (17), a method
The body of the function is defined somewhere else. As far as I know
this is used to fix different implementations for Dart VM in the
browser and Dart VM on the Server.
-Günter Zöchbauer

How to call a helper function from a library class [duplicate]

This question already has answers here:
How do you load a helper within a library in codeigniter?
(2 answers)
Closed 12 months ago.
I have a library class "Weekprint.php" (libraries/Weekprint.php) and in a helper file (calendar_helper.php) I have function like
print_calendar($start, $events, $cal_type).
I can use them separately,
I wonder if/how I can call print_calendar from Weekprint.
I use Codeigniter 3.1.5
You can call the helper from the library. The helper needs to be loaded before it can be used. Pick the logical spot in Weekprint.php to do that in the usual way.
$this->load->helper('calendar_helper');
After that, you can call it in the usual way when needed.
print_calendar($start, $events, $cal_type);
Obviously, the various arguments to the helper function need to be assigned values.
I could do it,
$CI = &get_instance();
$CI->load->helper('calendar_helper');
Inside the library Weekprint.php

Can we use the Date object in ES6 without the new keyword?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
Additions to the Date object
Date.prototype is an ordinary object (Firefox 41)
Does that mean we can finally use Object.create and call the constructor to get a functional date object, as opposed to using the new keyword? If so, how.
Does that mean we can finally use Object.create and call the constructor to get a functional date object, as opposed to using the new keyword? If so, how.
No, you can't. In fact, it's step in totally other direction - Date.prototype became plain object, and "magic" happens in constructor.
Because we are looking for a way to ban new across the board without exception.
You can use Reflect.construct (but it's just new operator as a function) or use Date factory methods: Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]]).
Hovewer, using new is recommended way to instantiate ES6 builtins and language standard is clearly moving to more classical approaches to creating instances of classes (you can't use Map or Set without new keyword).