我的FutureBuilder在CheckboxListTile中不断触发

时间:2019-06-27 05:51:57

标签: flutter

我有一个带有CheckboxListTile的列表。如果我选中或取消选中该列表,则会重新加载。
我使用AsyncMemoizer解决了这个问题。现在仅显示“正在加载...”,并且列表不会被加载

    class _MyHomePageState extends State<MyHomePage> {

      @override
      Widget build(BuildContext context) {
        var futureBuilder = new FutureBuilder(
          future: rest.fetchPost(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return new Text('loading...');
              default:
                if (snapshot.hasError)
                  return new Text('Error: ${snapshot.error}');
                else
                  return createListView(context, snapshot);
            }
          },
        );

    final AsyncMemoizer<List<Status>> _memoizer = 
     AsyncMemoizer<List<Status>>();
    Future<List<Status>> fetchPost(){
      return _memoizer.runOnce(() async {
      final response =
          await http.get(URL);
        if (response.statusCode == 200) {    
          List<Status> tmp = 
           Status.fromJsonList(json.decode(response.body));
        return tmp;
      } else {    
        throw Exception('Failed to load post');
      }
     }) ;
    }

0 个答案:

没有答案