Open In App

Dart 3.0 – Return Multiple Values From a Single Function

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the world of programming, returning multiple values from a function often comes with the baggage of extra code—whether it’s defining classes, managing lists, or working with maps. As developers, we crave code that is not just functional but also concise and elegant. Dart 3 transforms the way we handle multiple return values with its latest feature, Records. No longer do we need to clutter our code with additional structures; Dart 3 empowers us to return multiple values in a single stroke, without adding an extra line of code.

Let us explore the art of streamlined coding, where complexity yields simplicity, and where your code gains both clarity and efficiency. Dart 3 is here to make programming elegant, and we’re here to show you how. Programming is made simple!

Let us consider a use case where we need to print two values value1 and value2 by calling a function. The first function returns values having the same data types and the second function returns values having different datatypes.

Returning multiple values using class and object concept

Typically, you can create a class to encapsulate the returned values and then use an instance of that class to return and access the values. Here’s how you can do it

Dart




class MultipleValues {
    
  final int value1;
  final int value2;
  
  MultipleValues(this.value1, this.value2);
}
  
class DifferentDataTypes {
  final int integer;
  final String stringValue;
  
  DifferentDataTypes(this.integer, this.stringValue);
}
  
void main() {
  final multipleValues = returnMultipleValues();
  print(multipleValues.value1);
  print(multipleValues.value2);
  
  final differentDataTypes = returnDifferentDatatypes();
  print(differentDataTypes.integer);
  print(differentDataTypes.stringValue);
}
  
// Function returning multiple values using a custom class
MultipleValues returnMultipleValues() {
  return MultipleValues(1, 2);
}
  
// Function returning multiple values with 
// different data types using a custom class
DifferentDataTypes returnDifferentDatatypes() {
  return DifferentDataTypes(2, "This is a string");
}


As we can see , the above code is too lengthy and confusing for a beginner.

Returning multiple values using List

The same functionality of returning and accessing multiple values can be implemented using lists instead of a custom class. Here’s the code

Dart




void main() {
  final List<int> values1 = returnMultipleValues();
  print(values1[0]);
  print(values1[1]);
  
  final List<dynamic> values2 = returnDifferentDatatypes();
  print(values2[0]);
  print(values2[1]);
}
  
// Function returning multiple 
// values as a List of integers
List<int> returnMultipleValues() {
  return [1, 2];
}
  
// Function returning multiple values
// as a List of different data types
List<dynamic> returnDifferentDatatypes() {
  return [2, "This is a string"];
}


This Dart code demonstrates how to return and access multiple values using lists, simplifying the process of handling multiple return values without the need for custom classes.

But what if there exists much more simpler method even without using Lists. Introducing the power of Records in Dart 3, which offers a more streamlined approach to handling multiple return values without the use of Lists or custom classes. This feature enhances code simplicity and readability and it also ensures sound null safety.

Returning multiple values using Records : Dart 3

Programming just got simpler, and precision became effortless, thanks to Dart 3’s Records.

Dart




void main() {
  final (value1,value2)=returnMultipleValues();
  print(value1);
  print(value2);
  final (integer,string_value)=returnDifferentDatatypes();
  print(integer);
  print(string_value);
}
  
// function
(int,int) returnMultipleValues(){
  return(1,2);
}
  
// Function returning values having 
// different datatypes in a single line
(int,String)  returnDifferentDatatypes(){
    return(2,"This is a string");
}


This code utilizes a tuple-like syntax, which is inherently more straightforward and concise. It avoids the need for custom classes or lists, resulting in cleaner and more readable code, especially when dealing with multiple return values.

Dart 3 has arrived, and it brings a plethora of exciting features that promise to revolutionize your Dart programming experience. While we’ve explored one aspect of its simplicity in handling multiple values, Dart 3 offers much more . From enhanced performance to improved null safety, and the introduction of features like Records and more, the Dart ecosystem has never been more enticing.

As you dive into Dart 3, prepare to be amazed by the newfound power and simplicity it offers. These remarkable features that can elevate your coding skills and productivity. Dart 3 is a game-changer!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads