我有幾個關于顫振的 supabase 問題。我正在嘗試獲取通過他的 id 連接的人的用戶名。
這是我所做的:
getUsername() async {
final user = supabase.auth.currentUser;
print(user!.id);
var response = await supabase
.from("profiles")
.select("username")
.eq("id", user!.id)
.execute();
print(response.data);
return response.data.toString();
}
print(response.data) 回傳我:[{username:test}]
當我嘗試在我的應用程式上顯示它時,這就是我得到的 [screenshot][1]
我的列印代碼:
child: Text(
'$getUsername',
除了糾正在我的文本中顯示奇怪代碼的問題之外,我只想擁有用戶名(所以“test”)而不是 [{username: ...}]
謝謝![1]:https ://i.stack.imgur.com/AEp5O.png
uj5u.com熱心網友回復:
你給 Text 小部件一個函式參考本身而不是函式結果。
編輯:預加載用戶名
在類(主頁)中創建一個變數,它需要是StatefulWidget
late Future<String> username;
@override
void initState() {
// now use this in the FutureBuilder
username = getUsername();
super.initState();
}
首先更新getUsername這個:
Future<String> getUsername() async {
final user = supabase.auth.currentUser;
final response = await supabase.from("profiles")
.select("username")
.eq("id", user!.id)
.execute();
return response.data![0]['username'];
}
嘗試這個:
child: FutureBuilder<String>(
future: username,
builder: (context, snapshot) {
if (snapshot.ConnectionState != ConnectionState.done) {
return const CircularProgressIndicator();
} else if (!snapshot.hasError && snapshot.hasData) {
return Text(snapshot.data!);
} else {
return Text('error');
}
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497252.html
