转换Stream< List< String>>列出< String>扑朔迷离 [英] convert Stream<List<String>> to List<String> in flutter

查看:103
本文介绍了转换Stream< List< String>>列出< String>扑朔迷离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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层时,您将以ListList身份返回.返回类型应为Future<List<String>>.

The issue seems to be with your return type for categoryList. You're returning as List of Lists 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&lt; List&lt; String&gt;&gt;列出&lt; String&gt;扑朔迷离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆