我在顫振中有以下代碼,它顯示了具有排名的名稱串列。一切正常,但我想訪問lastName. 當我這樣做時,拋出錯誤Bad State: No element。
class ThisMonthTab extends StatelessWidget {
const ThisMonthTab({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: StreamBuilder<List<UserModel>>(
stream: PlayersRepo.fetchThisMonthPlayersList(),
builder: (context, snapshot) {
List<UserModel>? list = snapshot.data;
if (snapshot.connectionState == ConnectionState.waiting) {
return getLoader();
}
if (snapshot.data!.isEmpty) {
return emptyPageMessage(
LocaleKeys.noProgressThisMonth.tr());
}
if (snapshot.hasData) {
log(snapshot.data!.length.toString());
return ListView.builder(
shrinkWrap: true,
itemCount: list?.length,
padding: const EdgeInsets.only(top: 10),
itemBuilder: (context, index) => LeaderboardTileWidget(
imageUrl: list![index].avatarUrl,
title:
'${list[index].firstName} ${list[index].lastName.characters.first}.', //Error is here in this line for accessing the first character of last name
subtitle:
'${LocaleKeys.level.tr()} ${list[index].currentLevel} (${list[index].currentRankName.characters.first}), ${list[index].monthlyXP.gainedXP} XP',
isUser: list[index].id ==
context.read<UserModelProvider>().user.email,
rankNo: index 1,
),
);
} else {
return emptyPageMessage(LocaleKeys.fetchError.tr());
}
}),
),
const SizedBox(height: 10),
Text(
LocaleKeys.leaderboardDescription.tr(),
style: Theme.of(context).textTheme.subtitle1?.copyWith(
fontSize: 9, color: Colors.grey.shade600, letterSpacing: 0.4),
),
const SizedBox(height: 70)
],
),
);
}
}
誰能弄清楚這里的錯誤是什么,因為如果我直接顯示它,list[index].lastName它作業得很好。
uj5u.com熱心網友回復:
如果你只想要姓氏的第一個字符,為什么不走JS的方式,只需改變
list[index].lastName.characters.first
至
list[index].lastName[0]
為了安全起見,您可能希望將其轉換為Stringfirst 為
list[index].lastName.toString()[0]
原因是,在 EOD 時,aString只是一個ArrayofCharacters和 by [0],我們選擇了它的第一個專案。您可以在Dart Playground運行并檢查它。
uj5u.com熱心網友回復:
我剛剛通過檢查 if 解決了錯誤lastName is Empty or Not。在名稱串列中,名稱沒有姓氏的地方被破壞了。
我已更改list[index].lastName.characters.first為list[index].lastName.isEmpty ? " " : list[index].lastName.characters.first
希望這可以幫助某人。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516457.html
標籤:扑安卓工作室镖列表显示
