有没有办法在InitState方法上加载异步数据? [英] Is there a way to load async data on InitState method?

查看:321
本文介绍了有没有办法在InitState方法上加载异步数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在InitState方法上加载异步数据的方法,在build方法运行之前,我需要一些数据。我正在使用GoogleAuth代码,并且需要执行构建方法直到Stream运行。

I'm a looking for a way to load async data on InitState method, I need some data before build method runs. I'm using a GoogleAuth code, and I need to execute build method 'till a Stream runs.

我的initState方法是:

My initState method is:

 @override
  void initState () {
    super.initState();
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account)     {
      setState(() {
        _currentUser = account;
      });
    });
    _googleSignIn.signInSilently();
  }

我将不胜感激。

推荐答案

您可以使用 StreamBuilder 来执行此操作。每当中的数据更改时,此操作就会运行 builder 方法。

You can use StreamBuilder to do this. This will run the builder method whenever the data in stream changes.

以下是其中一个的代码段我的示例项目:

Below is a code snippet from one of my sample projects:

StreamBuilder<List<Content>> _getContentsList(BuildContext context) {
    final BlocProvider blocProvider = BlocProvider.of(context);
    int page = 1;
    return StreamBuilder<List<Content>>(
        stream: blocProvider.contentBloc.contents,
        initialData: [],
        builder: (context, snapshot) {
          if (snapshot.data.isNotEmpty) {
            return ListView.builder(itemBuilder: (context, index) {
              if (index < snapshot.data.length) {
                return ContentBox(content: snapshot.data.elementAt(index));
              } else if (index / 5 == page) {
                page++;
                blocProvider.contentBloc.index.add(index);
              }
            });
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        });
  }

在上面的代码 StreamBuilder 中,侦听任何更改在内容中,最初是一个空数组,并显示 CircularProgressIndicator 。一旦我进行API调用,提取的数据就会添加到内容数组,它将运行 builder 方法。

In the above code StreamBuilder listens for any change in contents, initially its an empty array and shows the CircularProgressIndicator. Once I make API call the data fetched is added to contents array, which will run the builder method.

当用户向下滚动时,更多提取内容并将其添加到内容数组,该数组将再次运行 builder 方法。

When the user scrolls down, more content is fetched and added to contents array which will again run builder method.

在您的情况下,只需要初始加载即可。但这提供了一个选项,可以在屏幕上显示其他内容,直到获取数据为止。

In your case only initial loading will be required. But this provides you an option to display something else on the screen till the data is fetched.

希望这会有所帮助。

编辑:

在您的情况下,我猜它看起来将如下所示:

In your case I am guessing it will look something like shown below:

StreamBuilder<List<Content>>(
        stream: account, // stream data to listen for change
        builder: (context, snapshot) {
            if(account != null) {
                return _googleSignIn.signInSilently();
            } else {
                // show loader or animation
            }
        });

这篇关于有没有办法在InitState方法上加载异步数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆