Flutter-从孩子更新父状态

时间:2018-07-13 10:30:03

标签: dart flutter state

我无法从孩子那里更新父状态。子窗口小部件具有名为“ onDragEnd”的回调,一旦完成对网格单元的拖放操作,就会调用该回调。这时,父级(网格)应根据新排序项重建UI,但是什么也没有发生。如果我在'setState()'之前设置断点,则'elems'属性包含正确排序的正确项目。即使替换整个列表,UI也不会更新。有什么建议么?我想念什么?

class _CustomGridState extends State<CustomGrid> { 

    List<Pack> elems;

    _CustomGridState(this.elems);

    @override
    Widget build(BuildContext context) {
         return GridView.count(
                crossAxisCount: 3,
                children: this
                  .elems
                  .map((pack) => PackFingernail(pack: pack,

                       onDragEnd: (Pack source, Pack target) {

                            var sourceIndex = this.elems.indexWhere((p) => p.id == source.id);
                            var targetIndex = this.elems.indexWhere((p) => p.id == target.id);

                            this.elems.removeAt(sourceIndex);
                            this.elems.insert(targetIndex, source);

                            setState(() {}); // Not working <-----
                       })

                   ).toList()
               );
   }
 }

1 个答案:

答案 0 :(得分:0)

尝试为您的public function index() { $users = DB::select('SELECT * FROM users'); dd($users); } 添加密钥

PackFingernail

// constructor PackFingernail(..., {Key key}): super(key:key) // in gridview .map((pack) => PackFingernail(pack: pack, onDragEnd: (Pack source, Pack target) { ... }, key: Key(pack.id.toString())) ).toList() 可以帮助重建Key