To convert a set of strings to a set of floats in Dart, you can use the map method to transform each string into a float and then create a new set from the resulting iterable.
In this example,
stringSet1
using the {}
syntax, which contains the string values '1.1'
, '2.2'
, and '3.3'
.map
method on stringSet1
to transform each string into a float using the double.parse
function.toSet
method, resulting in a set of floats named floatSet1
.print
.void main() {
var stringSet1 = {'1.1', '2.2', '3.3'};
var floatSet1 = stringSet1.map((str) => double.parse(str)).toSet();
print(floatSet1);
}
{1.1, 2.2, 3.3}
In this example,
stringSet2
using the {}
syntax, which contains the string values '4.4'
, 'hello'
, '5.5'
, '6.6'
, and 'world'
.map
method on stringSet2
to transform each string into a float using the double.parse
function.toSet
method, resulting in a set of floats named floatSet2
.print
.void main() {
var stringSet2 = {'4.4', 'hello', '5.5', '6.6', 'world'};
var floatSet2 = stringSet2.map((str) => double.parse(str)).toSet();
print(floatSet2);
}
{4.4, 5.5, 6.6}
In this tutorial, we learned How to Convert a Set of Strings to a Set of Floats in Dart language with well detailed examples.