Skip to main content

Constructors

caution

Always declare contructors with Named Parameters

class MyHomePage extends StatefulWidget {
MyHomePage( { Key key,this.title,this.counter,this.decrementCounter, required this.incrementCounter } );
// required means that parameter is must to give for user, other Named Parameters are Optional
/*
....
*/

Use Classes

  • when given widget is to be used again and again with same components but only different icons or text then use : Composition in the form of creating a Method of return type of that WidgetName with Named Parameters and can call that method from anywhere we want with different parameter values
    • refer to Extract Method in Composition page

Parameters of a Method

  • Named Parameter [Optional or required]
  • Simple parameter [rquired always when calling that method]
  • Positional [always Optional]

Named Parameters ✅ { .. } ✅✅

Always use Named Parameters

Named Parameters help us to avoid confusion while passing value for a function which has many parameter. Also the order of calling parameters doesn't matter

caution
  • Named parameter will be disclosed with curly bracket { ... }
  • have to use parameter name to assign a value which separated with colon : , like: findVolume(10,20,height:30); where height:30 means height is named parameter and so when calling that parameter its parameterName:valueToGive is to be used
  • Named Parametersr are optional or required.
  • Order of calling parameters doesn't matter

https://dart.dev/codelabs/dart-cheatsheet#named-parameters

Named parameters are optional unless they're explicitly marked as required.


getHttpUrl(String server, String path, {required int port = 80, int numRetries = 3}) {
/// server, path are MUST as they are normal parameters ;; `port` is [required] Named parameter so `port` MUST be called, `numRetries` is Optional Named parameter
// .....
}
{calling
getHttpUrl('example.com', '/index.html');   /// ERROR as `port` is `required`
getHttpUrl('example.com', '/index.html', port: 8080); // ☑️
getHttpUrl('example.com', '/index.html', port: 8080, numRetries: 5); // ☑️
getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080); // ☑️
getHttpUrl('example.com', '/index.html', numRetries: 5); /// ERROR as `port` is `required`
class MyHomePage extends StatefulWidget {
MyHomePage(
{Key key,this.title,this.counter,this.decrementCounter,this.incrementCounter}
)

required and null values for Named Parameters

When given null value or not initialised with any value with th Named Parameter

Solution: missing_default_value_for_parameter

> Error:
The parameter 'hello' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
>> Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter

As the parameter can’t be null, then either provide a default value:

void f([int x = 1]) {}
void g({int x = 2}) {}

or make the parameter a required parameter:

void f(int x) {}
void g({required int x}) {}

Other types of Constructors

Named Constructors

Factory Constructors

Redirecting Constructors


❌❌ Other Parameters ❌❌

Not Recommended

Simple Parameters

these parameters MUST to be declared when calling the method

findVolume(int length, int breath, int height) {
print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,30); // calling that method

❌❌ Positonal parameters [ .. ] ❌❌

caution

Positonal parameters always Optional

parameter will be disclosed with square bracket [ ... ]

DONT USE THEM

findVolume(int length, [int breath], [int height]) { // `length` is MUST, `breadth` and `height` is OPTIONAL
print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,30);//valid
findVolume(10);//also valid /// `breadth` and `height` are Optional So, are not given here