如何在Flutter中将FutureIndicator与FutureBuilder一起使用?

问题描述 投票:4回答:2

FutureBuilder完成加载时,我正在试图找出一种方法来向周围的班级表明。 RefreshIndicatorFuture作为参数,并在Future完成时停止显示刷新指示器。我没有访问传递给Future的完全相同的FutureBuilder变量,特别是当它们在两个单独的类中时,除非我可以将引用传递给一个,当它在另一个类中完成时,我会知道。 ..

dart flutter
2个回答
2
投票

我也在寻找这个答案。最后我想通了......

这是我的表现

FutureBuilder<String>(
  future: _calculation, // a previously-obtained Future<String> or null
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        return Text('Press button to start.');
      case ConnectionState.active:
      case ConnectionState.waiting:
        return Text('Awaiting result...');
      case ConnectionState.done:
        if (snapshot.hasError)
          return Text('Error: ${snapshot.error}');
        return  new RefreshIndicator(
                    key: _refreshIndicatorKey,
                    color: Colors.blue,
                    onRefresh: () {             
                      return _calculation= getCalculation(); // EDITED
                    },
               child: SingleChildScrollView(
              SingleChildScrollView 
                      physics: AlwaysScrollableScrollPhysics(), 
                      child: Text('Result: ${snapshot.data}')
               )


              );
           break;
           default:
             return null; 
    }

  },
) 

Future<String> getCalculation() async {
         /*Write your API here or what ever u want to get when pull to refresh*/
return null;
}  

0
投票

你必须访问快照,提供builder parm:

因此,snapshot.data为您提供了Future。

new FutureBuilder<String>(
  future: _calculation, // a Future<String> or null
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.none: return new Text('Press button to start');
      case ConnectionState.waiting: return new Text('Awaiting result...');
      default:
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');
        else
          return new Text('Result: ${snapshot.data}');
    }
  },
)

示例:https://flutter.io/cookbook/networking/background-parsing/

Doc:https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html

© www.soinside.com 2019 - 2024. All rights reserved.