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

时间:2020-04-01 14:23:48

标签: flutter firebase-realtime-database dart

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

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]);
                }

1 个答案:

答案 0 :(得分:0)

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

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

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

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
    ); 
  }
}

这是这种方法的输出

Output

这可以完成这项工作,但是,您可能需要为需要uid的每个类都这样做。

======================================

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

// 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
  }
}

在欢迎屏幕上:

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,就可以像这样直接访问它:

String uid = UserManager.uid;

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

希望这会有所帮助!