简体   繁体   English

如何转换列表<int>列出<float> Flutter?</float></int>

[英]How to convert List<int> to List<Float> with Flutter?

I have a function that returns List But in my case I want to read and display float values.我有一个返回列表的 function 但在我的例子中我想读取和显示浮点值。 However, this function is a system function I can't update it.但是,这个function是系统function我更新不了。

My question is how to convert List to List?我的问题是如何将列表转换为列表?

This is the code:这是代码:

characteristic.value.listen((event) async {
  var bleData = SetupModeResponse(data: event);
});

Event is by default a List.事件默认是一个列表。 When I try to declare data as List;当我尝试将数据声明为列表时; I got List cannot assigned to List.我得到的列表无法分配给列表。

I would be very thankful if you can help me.如果你能帮助我,我将不胜感激。

you can use the map method on list like that:您可以像这样在列表中使用map方法:

 List<int> intList = [1, 2, 3];
 List<double> doubleList = intList.map((i) => i.toDouble()).toList();

You can learn more about dart list mapping here map method您可以在此处了解有关 dart 列表映射的更多信息map 方法

This should also work:这也应该有效:

List<int> ints = [1,2,3];
List<double> doubles = List.from(ints);

Yo can try this method and see if it works你可以试试这个方法,看看是否有效

List<int> num = [1,2,3];
List<double> doubles = List.from(num);

Try the following code:试试下面的代码:

List<double> doubleList = event.map((i) => i.toDouble()).toList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM