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

ParameterOptional/RequiredDescription
namerequiredThe name of the environment declaration.
defaultValueoptionalThe default value to return if the environment declaration is not found.


✐ Example

1 Environment variable for a number

In this example,

  1. We assign the environment variable name 'MY_NUMBER' to the string variable envName.
  2. We retrieve the integer value of this environment variable using int.fromEnvironment() with a default value of 42.
  3. 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.