future builder keeps rebuilding on every setstate

Deadly 提交于 2020-12-07 09:56:12

问题


I am building an app which uses an api and I am using the future builder to fetch the data but the problem is when the state changes it rebuilds and I want to prevent this from happen.

Thanks,


回答1:


try using this :

class Example extends StatefulWidget {
  @override
 _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
 Future<response> future;

 @override
 void initState() {
   future = _asyncmethodCall();
   super.initState();
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
  future: future,
  builder: (context, snapshot) {
    // create some layout here
  },
 );
 }

Future<someResponse> _asyncmethodCall() async {
 // async code here
 }
}

similar question: How to deal with unwanted widget build?



来源:https://stackoverflow.com/questions/60990519/future-builder-keeps-rebuilding-on-every-setstate

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