转换Stream< List< String>>列出< String>扑朔迷离 [英] convert Stream<List<String>> to List<String> in flutter
问题描述
我正在尝试在Flutter中转换Stream<List<String>> to List<String>
这是我的代码
I am trying to convert a Stream<List<String>> to List<String>
in flutter
here is my code
Stream<List<String>> _currentEntries;
/// A stream of entries that should be displayed on the home screen.
Stream<List<String>> get categoryEntries => _currentEntries;
_currentEntries
被数据库中的数据填充.
我想将_currentEntries转换为List<String>
_currentEntries
is getting populated with data from a database.
i want to convert _currentEntries into List<String>
我尝试了以下代码,但不起作用
i tried the following code but doesn't work
List<List<String>> categoryList () async {
return await _currentEntries.toList();
}
我收到以下错误:
类型为List>的值不能从方法categoryList返回,因为它的返回类型为List>
A value of type List> can't be returned from method categoryList because it has a return type of List>
有人可以帮助您解决此问题并将Stream<List<String>
转换为List<String>
吗?
Can someone help how to solve this issues and convert a Stream<List<String>
to List<String>
?
推荐答案
问题似乎出在categoryList
的返回类型上.当Stream
仅包含单个List
层时,您将以List
的List
身份返回.返回类型应为Future<List<String>>
.
The issue seems to be with your return type for categoryList
. You're returning as List
of List
s when the Stream
only contains a single layer of List
. The return type should be Future<List<String>>
.
除了await
之外,还使用.first
,.last
或.single
获得单个元素,并且应删除toList()
.
Use .first
, .last
, or .single
in addition to await
to get just a single element, and toList()
should be removed.
Future<List<String>> categoryList () async {
return await _currentEntries.first;
}
还有一个快速提示:Dart会自动为所有字段生成getter和setter,因此您显示的getter方法不是必需的.
这篇关于转换Stream< List< String>>列出< String>扑朔迷离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!