Dart int.fromEnvironment()
Syntax & Examples
int.fromEnvironment constructor
The `int.fromEnvironment` constructor in Dart returns the integer value of the given environment declaration name.
Syntax of int.fromEnvironment
The syntax of int.int.fromEnvironment constructor is:
int.fromEnvironment(String name, { int defaultValue })
This int.fromEnvironment constructor of int returns the integer value of the given environment declaration name.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
name | required | The name of the environment declaration. |
defaultValue | optional | The default value to return if the environment declaration is not found. |
✐ Example
1 Environment variable for a number
In this example,
- We assign the environment variable name
'MY_NUMBER'
to the string variableenvName
. - We retrieve the integer value of this environment variable using
int.fromEnvironment()
with a default value of 42. - We then print the value to standard output.
Dart Program
void main() {
String envName = 'MY_NUMBER';
int myNumber = int.fromEnvironment(envName, defaultValue: 42);
print('Value of $envName: $myNumber');
}
Output
Value of MY_NUMBER: 42
Summary
In this Dart tutorial, we learned about int.fromEnvironment constructor of int: the syntax and few working examples with output and detailed explanation for each example.