我有getData()來自 API 的資料,并且 UI 中顯示了一個資料串列,并且串列中的每個專案都是可點擊的。所以想在 for 回圈中添加一個 bool 型別的變數,i但我不知道如何將變數名與i. 以及這個布爾變數在_navigateSlide(BuildContext context)小部件中的作業。所以當我在for回圈中添加一個帶有索引的布爾變數時,i它向getData() 我
for (int i = 0; i < value.length; i ) { bool check[i] = false; }
顯示了一個錯誤
Illegal assignment to non-assignable expression. Missing selector such as '.identifier' or '[0]'.關于代碼check[i]
這是我的代碼:-
class ExploreAds extends StatefulWidget {
ExploreAds({Key? key}) : super(key: key);
@override
_ExploreAds createState() => _ExploreAds();
}
class _ExploreAds extends State<ExploreAds> {
bool show = false;
bool checkother = false;
final List<String> data = <String>[];
void addValue(txt) {
setState(() {
data.add('${txt}');
});
print(data);
}
void removeValue(rmtxt) {
setState(() {
data.remove('${rmtxt}');
});
print(data);
}
var catdata;
@override
void initState() {
super.initState();
getData();
}
getData() async{
await BuySellCatsController().buysellCatAPI().then((value) {
for (int i = 0; i < value.length; i ) {
bool check[i] = false;
}
}
);
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final ThemeData themeData = Theme.of(context);
const double padding = 25;
const sidePadding = EdgeInsets.symmetric(horizontal: padding);
return Scaffold(
body: Container(
height: 550,
child: catdata != null?
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
addVerticalSpace(10),
Expanded(
child : Container(
child: GridView(
padding: EdgeInsets.only(right:8,),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
childAspectRatio: 0.70,
),
children: List.generate(catdata.length 1,
(index) => index == catdata.length ?
Stack(
children: [
Container(
padding: EdgeInsets.only(right:5, left:5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Color(0xffacacac)),
),
child: InkWell(
onTap: () => setState(
() {
checkother = !checkother ;
checkother ? addValue('Others') : removeValue('Others');
},
),
child: Column(
children: [
Image.asset('assets/icons/pngwing49.png', width: 115, height: 115),
Text('Others')
],
)
),
),
Visibility(
visible: check6 ? true : false,
child: Positioned(
top: 5,
right: 5,
child: Image.asset('assets/icons/tick.png', width: 15,),
)
)
],
):
Stack(
children: [
Container(
padding: EdgeInsets.only(right:5, left:5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Color(0xffacacac)),
),
child: InkWell(
onTap: () => setState(
() {
check[index] = !check[index];
check[index]? addValue('${catdata[index]['name']}') : removeValue('${catdata[index]['name']}');
},
),
child: Column(
children: [
Image.network('${catdata[index]['image']}', width: 115, height: 115,),
Text('${catdata[index]['name']}')
],
)
),
),
Visibility(
visible: check[index] ? true : false,
child: Positioned(
top: 5,
right: 5,
child: Image.asset('assets/icons/tick.png', width: 15,),
)
)
],
)
),
),
),
),
addVerticalSpace(10),
Padding(
padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
child: ElevatedButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
style: ElevatedButton.styleFrom(
elevation: 6,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(80.0)),
padding: const EdgeInsets.all(0.0),
),
child: Ink(
decoration: const BoxDecoration(
gradient: LinearGradient(colors: [Color(0xfff9568f), Color(0xfffabaae)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
child: Container(
constraints: const BoxConstraints(minWidth: 88.0, minHeight: 50.0), // min sizes for Material buttons
alignment: Alignment.center,
child: const Text(
'Apply',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
),
),
)
],
):
const Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
strokeWidth: 2,
color: Color(0xffe93332),
)
),
)
);
}
}
請幫助我該怎么做?如果有人知道請幫助我。
uj5u.com熱心網友回復:
您正在回圈內宣告和分配一個變數。從您的代碼中,我假設您想要獲取一個 bool 值串列,其長度應等于 value.length - 全部設定為 fale。注意 - 當您已經在使用 await 時,無需使用 .then。
你應該有這樣的東西:
Future<List<bool>> getData() async{
var value = await BuySellCatsController().buysellCatAPI();
return List.filled(value.length, false);
}
如果你真的想使用 .then 和 for 回圈,下面是它的作業方式
Future<List<bool>> getData() async{
return await BuySellCatsController().buysellCatAPI().then((value) {
var check=<bool>[];
for (int i = 0; i < value.length; i ) {
check.add(false);
}
}
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523294.html
標籤:安卓扑镖颤振布局颤振依赖
上一篇:collectAsStateWithLifecycle是否只適用于冷流,對熱流沒有幫助(例如stateFlow)?
