Multiple constructor in flutter - flutter

I try to add multiple constructor in my code but it shows error. help me to solve this.
code :
class NoteModify {
const NoteModify({Key? key}) : super(key: key);
String NoteID;
NoteModify(String x) {
NoteID = x;
}
}
I need to use both constructor. Because I am working with 2 buttons and one button for navigate without sending data to another activity and one button for navigate with data to another activity.

As #Jigar Fumakiya said in his answer, dart doesn't support overloading.
You need to use differently named constructors to fix the error
NoteModify is already declared in this scoped
However, you have another issue, you declared a const constructor and String NoteID is not a final. To declare a const constructor, all the fields must be final.
That is why you get the error
Error: Constructor is marked as 'const' so all fields must be final
If you need the const constructor:
class NoteModify{
const NoteModify({Key? key}) : NoteID = null, super(key: key);
NoteModify.formId({this.NoteID});
final String? NoteID;
}
If you need NoteID to be a variable instead, you'll have to remove the const keyword:
class NoteModify{
NoteModify({Key? key}): super(key: key);
NoteModify.formId({this.NoteID});
String? NoteID;
}

Dart doesn't support methods/constructor overloading. But you can have multiple named constructor
Here
class NoteModify{
String NoteID;
NoteModify({Key key}) {
// main constructor
}
NoteModify.formId({this.NoteID}){
//Here is the named constructor
}
}

Related

Passing Arguments to super from the widget in flutter

Im trying to pass arguments from my widget's state into a super class, but i cannot access the "widget." from the initialization list.
if i do pass it from the variables and accept an additional parameter in the state's ctor, i get the lint error:
no_logic_in_create_state
Here's my code:
class TransferPage extends View {
final String screenId;
TransferPage(this.screenId, {Key? key}) : super(key: key);
#override
// ignore: no_logic_in_create_state
State<TransferPage> createState() => TransferPageState(screenId);
}
class TransferPageState extends ViewState<TransferPage, TransferController> {
final String screenId;
TransferPageState(this.screenId)
: super(TransferController(GetTransferDataUsecaseParams(screenId)));
I want to pass the id into the "super" ctor,
What's the best way to go about it?

The default constructor is already defined. im using flutter [duplicate]

This question already has answers here:
Dart Multiple Constructors
(10 answers)
How can I create multiple constructors in dart?
(3 answers)
Closed 2 years ago.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}
The default constructor is already defined.
Try giving one of the constructors a name.dart(duplicate_constructor)
i want this two construters to pass on any one can help me in his
You're getting the error because you're trying to make two default constructor. Try making the second one a named constructor to fix the issue.
Note : Dart doesn't support's constructor and method overloading. That's why it comes with named methods that makes them more readable and easy to manage.
class HomePage extends StatefulWidget {
final String uid;
HomePage({Key key, #required this.uid}) : super(key: key);
final FirebaseUser user;
HomePage.user({this.user});
#override
_HomePageState createState() => _HomePageState(uid);
}

how does below constructor work in dart, I have extracted widget and flutter has given the below constructor for my widget

ExtractedWidgetForLst({
Key key,
#required List<Expense> expensesData,
}) : _expensesData = expensesData, super(key: key);
If we need any argument to be passed to the class, we use parameterized constructor and in that we use super() to call parent constructor inside the body if needed. and if we don't need calling super then just we use this keyword in the parameter itself, e.g:
class MyClass{
String name;
String surname;
MyClass({this.name,this.surname});
}
but what does operator ' : ' and super means over this scenario.
: is for the parameter and super(key: key); calls the Constructor from the inherited Widget class

Dart: Inherit method from abstract class

Trying to make a generic route "base class", where an abstract class defines a getter that returns the route name. Something like this:
abstract class ScreenAbstract extends StatefulWidget {
static String name;
static String get routeName => '/$name';
ScreenAbstract({Key key}) : super(key: key);
}
Then, any "screen" widget can extend this class:
class SomeScreen extends ScreenAbstract {
static final name = 'someScreen';
SomeScreen({Key key}) : super(key: key);
#override
_SomeScreenState createState() => _SomeScreenState();
}
Which should then be accessible like this:
Navigator.of(context).pushNamed(SomeScreen.routeName);
Hoever, when trying that, the linter throws an error:
The getter 'routeName' isn't defined for the type 'SomeScreen'.
What am I doing wrong?
In dart there's no inheritance of static members. See Language Specification here-
Inheritance of static methods has little utility in Dart. Static
methods cannot be overridden. Any required static function can be
obtained from its declaring library, and there is no need to bring it
into scope via inheritance. Experience shows that developers are
confused by the idea of inherited methods that are not instance
methods.
Of course, the entire notion of static methods is debatable, but it is
retained here because so many programmers are familiar with it. Dart
static methods may be seen as functions of the enclosing library.
To tackle this, you can update your solution like this -
Abstract Parent Class -
abstract class ScreenAbstract extends StatefulWidget {
final String _name;
String get routeName => '/$_name';
ScreenAbstract(this._name, {Key key}) : super(key: key);
}
The Screen Widget that extends the Parent class -
class SomeScreen extends ScreenAbstract {
static final String name = "url";
SomeScreen({Key key}) : super(name, key: key);
#override
_SomeScreenState createState() => _SomeScreenState();
}
Then you can access it like this -
Navigator.of(context).pushNamed(SomeScreen().routeName);

Flutter calling StatefulWidget's constructor caused expression can't be evaluated

I'm creating a stateful widget, and trying to call the superclass construction using super(key: key. However, I'm getting an error saying:
class ArticlesPage extends StatefulWidget {
ArticlesPage({Key key}) {
super(key: key);
}
#override
_ArticlesPageState createState() => _ArticlesPageState();
}
For dart, super should be called in the initializer list. Ex:
ArticlesPage({Key key}) : super(key: key);
If you also want to initialize fields here, you should do this before the super call. super should always be last in the initializer list.
If you want to save key from --> Key key as a class variable also.
In this case, you will get a default GlobalKey value if it's not passed during constructor initialize.
AppStateFullWidget({Key key}):super(key: key ?? GlobalKey()){
_key = super.key;
}