基本上我正在尝试创建一个应用程序,其内容将使用从网站获取信息的异步函数进行更新,但是当我尝试设置新状态时,它不会重新加载新内容.如果我调试应用程序,它会显示当前内容是新内容,但在"重建"整个窗口小部件后,它不会显示新信息.
编辑:loadData()方法,基本上读取带有http包的URL,该URL包含一个JSON文件,其内容每5分钟更新一次新消息.例如,带有体育实时记分牌的.json文件,其得分总是在变化,因此内容应始终随新结果而变化.
class mainWidget extends StatefulWidget
{
State<StatefulWidget> createState() => new mainWidgetState();
}
class mainWidgetState extends State<mainWidget>
{
List<Widget> _data;
Timer timer;
Widget build(BuildContext context) {
return new ListView(
children: _data);
}
@override
void initState() {
super.initState();
timer = new Timer.periodic(new Duration(seconds: 2), (Timer timer) async {
String s = await loadData();
this.setState(() {
_data = <Widget> [new childWidget(s)];
});
});
}
}
class childWidget extends StatefulWidget {
childWidget(String s){
_title = s;
}
Widget _title;
createState() => new childState(); …Run Code Online (Sandbox Code Playgroud) 我有两个流,Stream<A>和Stream<B>.我有一个C带a A和a 的类型的构造函数B.如何将两者合并Stream为一个Stream<C>?
我的问题是关于与集团模式一起使用的导航.
在我的LoginScreen小部件中,我有一个按钮,可以将事件添加到集团的EventSink中.bloc调用API并验证用户身份.问题是在LoginScreen Widget中我必须监听流,以及在返回成功状态后如何导航到另一个屏幕.
希望我清楚自己.谢谢!
如果某些代码返回一个future并确定future将返回"Error"或"Exception",那么如何将堆栈跟踪传递给它 Completer.completeException(exception, stackTrace);
我正在将一些JavaScript移植到Dart.我有window.setTimeout一段代码用于在一段时间后运行回调.在某些情况下,该回调会被取消window.clearTimeout.
Dart的相当于什么?我可以new Future.delayed用来替换setTimeout,但我看不到取消它的方法.我也无法找到clearTimeoutDart的电话.
我有一个Iterable,我想将它转换为Stream.什么是最有效/最短的代码量?
例如:
Future convert(thing) {
return someAsyncOperation(thing);
}
Stream doStuff(Iterable things) {
return things
.map((t) async => await convert(t)) // this isn't exactly what I want
// because this returns the Future
// and not the value
.where((value) => value != null)
.toStream(); // this doesn't exist...
}
Run Code Online (Sandbox Code Playgroud)
注意:iterable.toStream()不存在,但我想要这样的东西.:)
在Dart中,有一个隔离概念.我有一个应用程序(我在Dart中进行实验),它有很多异步IO,每次调用(它们都是数据库调用)都依赖于前一个.所以我最终陷入了嵌套的回调地狱.
我想知道Isolates是否可以解决那个嵌套的回调汤,但它看起来有点冗长,我不确定它是否适合它.
在下一个ECMAScript Harmony中也提出了可以解决这些问题的Generators,但是你现在如何以干净的方式在Dart中做很多异步IO呢?
可以用Dart语言编写类似的代码吗?
int i;
try {
i = await getResultAsync();
} catch(exception) {
// Do something
}
Run Code Online (Sandbox Code Playgroud) 在Dart UI中,我有一个按钮[submit]来启动长异步请求.[submit]处理程序返回Future.接下来,按钮[submit]被按钮[cancel]替换,以允许取消整个操作.在[取消]处理程序中,我想取消长操作.如何取消提交处理程序返回的Future?我发现没有办法做到这一点.
Dart允许链接期货按顺序调用多个异步方法而不嵌套回调,这很棒.
假设我们想首先连接到像Redis这样的数据存储,然后运行一堆顺序读取:
Future<String> FirstValue(String indexKey)
{
return RedisClient.connect(Config.connectionStringRedis)
.then((RedisClient redisClient) => redisClient.exists(indexKey))
.then((bool exists) => !exists ? null : redisClient.smembers(indexKey))
.then((Set<String> keys) => redisClient.get(keys.first))
.then((String value) => "result: $value");
}
Run Code Online (Sandbox Code Playgroud)
四种异步方法,但代码相当容易阅读和理解.几乎看起来步骤是同步并按顺序执行的.美丽!(想象一下,必须使用嵌套的JavaScript回调编写相同的代码......)
不幸的是,这不会很有效:我们从.connect方法中获得的RedisClient 仅被分配给一个局部变量,该变量不在后续.thens的范围内.所以,redisClient.smembers和redisClient.get实际上将抛出一个空指针异常.
显而易见的解决方法是将返回值保存在具有函数范围的另一个变量中:
Future<String> FirstValue(String indexKey)
{
RedisClient redisClient = null;
return RedisClient.connect(Config.connectionStringRedis)
.then((RedisClient theRedisClient)
{
redisClient = theRedisClient;
return redisClient.exists(indexKey);
})
.then((bool exists) => !exists ? null : redisClient.smembers(indexKey))
.then((Set<String> keys) => redisClient.get(keys.first))
.then((String …Run Code Online (Sandbox Code Playgroud)