What is the equivalent code in flutter for “ mAdapter.notifyDataSetChanged();”;

邮差的信 提交于 2021-01-28 08:35:42

问题



Befor HTTP response show loading and after https response create tab view. I successfully create tab view but my problem is after HTTP request unable to update the view in the android studio with java we use

`mAdapter.notifyDataSetChanged();
 mActivity.runOnUiThread(new Runnable() {
                                         @Override
                                         public void run() {


                                         }
                                     });`


code for doing this. what is the equivalent code in the flutter?


回答1:


If you want update any widget in flutter you should call setState((){})

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isLoading = false;

  @override
  void initState() {
    super.initState();

    isLoading = true;
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return isLoading ? new CircularProgressIndicator() : // show list;
  }

  Future<ModelClass> getData() async {
    // get data from http
    ...
    // after processing response
    setState(() {
      isLoading = false;
    });
  }
}



回答2:


In Flutter, the UI is updated when the State is changed using setState method.

In your case, you will need some variables (E.x: isLoading, items (for your list)).

When the app loads data from backend, isLoading = true, items = [] (empty). When the request is done, isLoading = false and items = [item1, item2,...]. Here is where you have to use setState method to update your UI.

Under your build method, you have to use these 2 variables to render the corresponding UI.



来源:https://stackoverflow.com/questions/50619480/what-is-the-equivalent-code-in-flutter-for-madapter-notifydatasetchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!