这是解决问题的另一种方法。
这个想法是使用一个变量 _loading 并最初将其设置为 true。
现在,在您的 inputData() 函数之后,您可以在获得 dbref 后将其设置为 false。
存储 dbref,即我在下面的代码中存储 _myFuture 的方式,即在类中全局存储。
如果 _loading 变量为真,则使用您的 _loading 变量返回进度条,否则返回 FutureBuilder 并使用您的 dbref.once()。现在,您已经加载了它,此时它应该是可用的。
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 的班级都这样做。
=========================================
这是我在 cmets 中描述的方法。
// 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;
从而消除了期货的使用。
希望这会有所帮助!