Home > Software engineering >  Flutter concatenate bool variable with loop index
Flutter concatenate bool variable with loop index

Time:10-31

I have data in getData() from API and there is a list of data shown in UI and every item in the list is clickable. so want to add a bool type variable in for loop with the value i but I don't know how to concatenate the variable name with i. and the working of this bool variable in _navigateSlide(BuildContext context) widget. so when i add a bool variable in for loop with index i in getData() like for (int i = 0; i < value.length; i ) { bool check[i] = false; } it shows me an error of

Illegal assignment to non-assignable expression. Missing selector such as '.identifier' or '[0]'. on code check[i]

Here is my code:-

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),
            )
          ),
        )
);
}

}

please help how can I do this? if anyone knows please help me.

CodePudding user response:

You are declaring and assigning a variable inside the loop. From your code, I'm assuming you want to get a list of bool values, that should have the length equal to value.length - all set to fale. Note - no need to use .then when you are already using await.

You shoud have something like this:

Future<List<bool>> getData() async{
    var value = await BuySellCatsController().buysellCatAPI();
    return List.filled(value.length, false);
}

If you really wanted to use .then and for loop, here's how it would work

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);
  }
}
);
}
  • Related