首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在FutureBuilder (builder:)中引用方法中的变量?

如何在FutureBuilder (builder:)中引用方法中的变量?
EN

Stack Overflow用户
提问于 2020-04-01 22:23:48
回答 1查看 342关注 0票数 0

我想在将来的构建器中使用inputData()中的变量dbRef :您可以在星号之间看到该变量。

代码语言:javascript
运行
复制
void inputData() async {
        FirebaseUser user = await FirebaseAuth.instance.currentUser();
        final uid = user.uid;
      final **dbRef** = FirebaseDatabase.instance.reference().child("Add Job Details").child(uid).child("Favorites");
      }

     @override
      Widget build(BuildContext context) {
        return FutureBuilder (

            future: **dbRef**.once(),
            builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
              if (snapshot.hasData) {
                List<Map<dynamic, dynamic>> list = [];
                for (String key in snapshot.data.value.keys) {
                  list.add(snapshot.data.value[key]);
                }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-02 01:01:33

这是解决这个问题的另一种方法。

这个想法是使用一个变量_loading,并在最初将其设置为true。现在,在您的inputData()函数中,一旦获得dbref,就可以将其设置为false。存储_myFuture,就像我在下面的代码中存储dbref一样,即在类中全局存储dbref。

如果_loading变量为真,则使用它返回一个进度条,否则返回带有dbref.once()的FutureBuilder。现在,您已经加载了它,现在应该可以使用它了。

代码语言:javascript
运行
复制
class MyWidget extends StatefulWidget {
  @override
  createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {

  // Is the future being loaded? 
  bool _loading;

  // This is the future we will be using in our FutureBuilder.
  // It is currently null and we will assign it in _loadMyFuture function.
  // Until assigned, we will keep the _loading variable as true.
  Future<String> _myFuture;

  // Load the _myFuture with the future we are going to use in FutureBuilder
  Future<void> _loadMyFuture() async {
    // Fake the wait for 2 seconds
    await Future.delayed(const Duration(seconds: 2));

    // Our fake future that will take 2 seconds to return "Hello"
    _myFuture = Future(() async {
      await Future.delayed(const Duration(seconds: 2));
      return "Hello";
    });
  }

  // We initialize stuff here. Remember, initState is called once in the beginning so hot-reload wont make flutter call it again
  @override
  initState() {
    super.initState();
    _loading = true; // Start loading
    _loadMyFuture().then((x) => setState(() => _loading = false)); // Set loading = false when the future is loaded
  }

  @override
  Widget build(BuildContext context) {
    // If loading, show loading bar
    return _loading?_loader():FutureBuilder<String>(
      future: _myFuture,
      builder: (context, snapshot) {
        if(!snapshot.hasData) return _loader(); // still loading but now it's due to the delay in _myFuture
        else return Text(snapshot.data);
      },
    );
  }

  // A simple loading widget
  Widget _loader() {
    return Container(
      child: CircularProgressIndicator(),
      width: 30,
      height: 30
    ); 
  }
}

下面是这种方法的输出

这可以完成这项工作,但是,您可能需要为需要uid的每个类执行此操作。

==

这是我在评论中描述的方法。

代码语言:javascript
运行
复制
// Create a User Manager like this
class UserManager {
  static String _uid;

  static String get uid => _uid;

  static Future<void> loadUID() async {
    // Your loading code
    await Future.delayed(const Duration(seconds: 5));
    _uid = '1234'; // Let's assign it directly for the sake of this example
  }
}

在您的欢迎屏幕中:

代码语言:javascript
运行
复制
class MyWidget extends StatefulWidget {
  @override
  createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool _loading = true;

  @override
  void initState() {
    super.initState();
    UserManager.loadUID().then((x) => setState(() => _loading = false));
  }

  @override
  Widget build(BuildContext context) {
    return _loading ? _loader() : Text('Welcome User ${UserManager.uid}!');
  }

  // A simple loading widget
  Widget _loader() {
    return Container(child: CircularProgressIndicator(), width: 30, height: 30);
  }
}

这种方法的优点是,一旦加载了uid,就可以直接访问它,如下所示:

代码语言:javascript
运行
复制
String uid = UserManager.uid;

从而消除了对期货的使用。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60973159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档