Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 为什么';等待&x27;无法在Dart中提供具有内置价值的结果?_Flutter_Dart - Fatal编程技术网

Flutter 为什么';等待&x27;无法在Dart中提供具有内置价值的结果?

Flutter 为什么';等待&x27;无法在Dart中提供具有内置价值的结果?,flutter,dart,Flutter,Dart,我感兴趣的是理解为什么第一个表达式无法更新我的状态,而第二个表达式可以工作。这段代码使用的是内置值和bloc yield this.state.rebuild( (b) async => b..things = await thingRepo.things(), ); List<Thing> _things = await thingRepo.things(); yield this.state.rebuild( (b) async => b..th

我感兴趣的是理解为什么第一个表达式无法更新我的状态,而第二个表达式可以工作。这段代码使用的是内置值和bloc

yield this.state.rebuild(
     (b) async => b..things = await thingRepo.things(),
);

List<Thing> _things = await thingRepo.things();
yield this.state.rebuild(
     (b) async => b..things = _things,
);
生成此.state.rebuild(
(b) async=>b..things=等待thingRepo.things(),
);
列出事物=等待事物repo.things();
让出这个.state.rebuild(
(b) 异步=>b..things=\U things,
);

已生成。重建是同步的。如果给出一个异步回调,它不会等待它完成

  • rebuild
    从原始
    builded
    值创建一个
    Builder
  • Builder
    被传递给回调函数,期望回调函数对其进行修改
  • rebuild
    然后返回从变异的
    Builder
    创建的新
    builded
    。请注意,回调返回的值将被忽略
  • 如果您的回调没有同步地改变
    Builder
    rebuild
    将返回原始值。这就是第一个示例中出现的错误:在变异
    Builder
    之前,回调
    wait
    s a
    Future
    。当突变发生时,已经太晚了:
    rebuild
    已经返回了一个
    builded

    在第二个示例中,您已将
    wait
    移到回调之外
    rebuild
    立即获取变异值,以便在返回的值中使用该值。请注意,即使第二种情况下的回调声明为
    async
    ,回调也会立即执行变异

    考虑以下情况:

    var x=MyBuiltValue((b)=>b
    …foo=0
    ..巴=0);
    x=x.rebuild((b)异步{
    b、 foo=123;
    等待未来。延迟(持续时间(秒:0));
    b、 bar=456;
    });
    

    您将得到
    x.foo
    成为
    123
    x.bar
    仍然是
    0

    非常好的解释和示例,谢谢。同步变异似乎是因为rebuild()方法中的cascade操作符不尊重async-@Wes,而第二种情况下的同步变异是因为调用异步函数时,立即输入该函数,并在该函数中同步执行,直到它需要等待(即,直到它返回
    Future
    )。它与级联运算符无关。