首页 > 解决方案 > 网络调用后如何修改 UI 以填充子小部件

问题描述

我对颤动相当陌生,请帮助我在执行网络呼叫后如何填充儿童小部件?

类 _BusResultScreenState 扩展状态 {

@override 小部件构建(BuildContext 上下文){

helper myHelper = new helper();
myHelper.fetch(apiArg).then ((response) {
  if(response['type']=="success") {
    // Sample data from server in response.feedback
    [
       {"name":"bus1","id":"54"},
       {"name":"bus2","id":"55"},
       {"name":"bus3","id":"56"}
     ]
     while(){
        SharedBusCard(arg1,arg2,...) // This has to go into children Widget
     }
  }
});

return Scaffold(
  backgroundColor: Colors.grey.shade200,
  body: SafeArea(
    child: Padding(
      padding: const EdgeInsets.all(12),
      child: Column(
        children: <Widget>[
          Material(
            elevation: 4,
            child: Container(
          ... code
          .... code
          SizedBox(height: 15),
          Expanded(
            child: Container(
              width: double.infinity,
              height: double.infinity,
              child: ListView(
                children: <Widget>[
                      //I wanted to display results here
                    SharedBusCard(arg1,arg2,...) // this is the card to be looped
                ],
              ),
            ),
          )
        ],
      ),
    ),
  ),
);

} }

谢谢你的帮助。

标签: flutterloops

解决方案


使用FutureBuilder小部件。这对网络调用很有用,您可以处理错误,在其中显示加载屏幕。这是文档和使用它的精彩视频。


推荐阅读