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

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

问题描述

我正在寻找一种在 InitState 方法上加载异步数据的方法,在构建方法运行之前我需要一些数据.我正在使用 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();
  }

我将不胜感激任何反馈.

I will appreciate any feedback.

推荐答案

方法 1 :您可以使用 StreamBuilder 来执行此操作.只要 stream 中的数据发生变化,这就会运行 builder 方法.

Method 1 : 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
            }
        });

方法 2: 另一种方法是创建一个 async 方法并从您那里调用它 initState() 方法,如下所示:

Method 2: Another method would be to create an async method and call it from you initState() method like shown below:

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

  void asyncMethod() async {
    await asyncCall1();
    await asyncCall2();
    // ....
  }

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

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