我撰寫了一個簡短的 Flutter 應用程式,它有一個異步函式,可以從云 Firestore 中獲取值,并根據資料庫中的資訊回傳 Future bool 型別。現在在我的主代碼中,我想根據回傳表單中提到函式的值回傳一個小部件,但是我沒有成功使用回傳的值。我嘗試撰寫一個外部異步函式,該函式將呼叫第一個函式并將回傳的值放入共享變數中。但它對我不起作用,有時會出現關于該變數未初始化的錯誤。
看起來代碼沒有逐行停止并等待我的函式在被呼叫時回傳值并繼續執行 if 陳述句。
有人知道如何根據函式回傳的值回傳小部件并解決該問題嗎?
我嘗試使用 then 的代碼塊,但我以前從未使用過它,所以它不能正常作業。
我的拳頭功能:
Future<bool> firstFunc(String uid) async {
DocumentSnapshot userData =
await FirebaseFirestore.instance.collection('Users').doc(uid).get();
bool value = userData["param"];
return value;
}
這是我的主要代碼:
// imports
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: AuthenticationWrapper(),
);
}
}
class AuthenticationWrapper extends StatefulWidget {
const AuthenticationWrapper({Key? key}) : super(key: key);
@override
_AuthenticationWrapperState createState() => _AuthenticationWrapperState();
}
class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
final FirebaseAuth _auth = FirebaseAuth.instance;
late bool sharedVar;
void getValue(String uid) async {
bool value = await firstFunc(uid);
sharedVar = value;
}
@override
Widget build(BuildContext context) {
if (_auth.currentUser != null) {
getValue(_auth.currentUser!.uid);
if (sharedVar == true)
{
return BossHomePage();
}
else {
return ClientHomePage();
}
} else {
return const EnterPhonePage();
}
}
}
uj5u.com熱心網友回復:
它不等待的原因getValue是getValuetype void。嘗試這個;
class AuthenticationWrapper extends StatefulWidget {
const AuthenticationWrapper({Key? key}) : super(key: key);
@override
_AuthenticationWrapperState createState() => _AuthenticationWrapperState();
}
class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
if (_auth.currentUser != null) {
return FutureBuilder<bool>(
future: firstFunc(uid),
builder: (BuildContext context, snapshot){
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const CircularProgressIndicator();
case ConnectionState.none:
return const Text('No data');
case ConnectionState.active:
case ConnectionState.done:
if (snapshot.data == true) {
return BossHomePage();
} else {
return ClientHomePage();
}
default:
return const Text('No data');
}
},
)
} else {
return const EnterPhonePage();
}
}
}
uj5u.com熱心網友回復:
getValue是一種未來的方法,你需要async在某個時候使用。您可以將其轉換為
Future<bool> getValue(String uid) async {
bool value = ....;
return value;
}
并在這種情況下使用FutureBuilder(或initState更好的 SatefullWidget)
late bool sharedVar;
Future<void> getValue(String uid) async {
bool value =...;
sharedVar = value;
}
bool? currentUser;
_initData()async{
getValue("");
///all others methods
}
@override
void initState() {
super.initState();
_initData();
}
更多關于異步等待
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431295.html
上一篇:交換機未正確更新
