0

This one probably has a very easy answer, but I've been blocked trying to solve this for a while and can't really get it to work.

**problem:**when I call checkBeans() with the input got1Beans on the initState, I still get the initial value (false) for got1Beans. What I need is to be able to update got1Beans = true. Anyone able to sort this beany situation? What am I missing here?

import 'package:flutter/material.dart';

class BeansExample extends StatefulWidget {
 @override
_BeansExampleState createState() => _BeansExampleState();
}

class _BeansExampleState extends State<BeansExample> {
 bool got1beans = false;
 bool got2beans = false;
 bool got3beans = false;
 bool got4beans = false;
 bool got5beans = false;


checkBeans(bool getBeans) {
setState(() {
  getBeans = true;           //shouldn't it update the input value???
});
}



 @override
 void initState() {
  super.initState();
  checkBeans(got1beans);   //here I call got1Beans
  print(got1beans);        //This prints false, need to get true here 

    }

  @override
  Widget build(BuildContext context) {
   return Container();
  }
  }

1 Answers1

0

checkBeans(value) doesn't mean this.gotXbeans = true/false. It means value = true/false. The variable "value" should be local or method variable.

You can update your code.

List<bool> beans = [false, false, false, false, false];

checkBeans(int index, bool value) {
  setState(() {
    beans[index] = value;
  });
}

Or you can create methods like this. checkBeans1, checkBeans2, checkBeans3, checkBeans4, checkBeans5...

checkBeans1(bool value) {
  setState(() {
    get1beans = value;
 });
}

Please check this question if you want to pass a reference.

loyal
  • 231
  • 1
  • 9
  • Thanks for helping me sort this beany situation. As I was expecting, it had to be a simple answer. I used your first approach to solve my problem(the second one would take way longer by writting all the methods manually, since I have over 50 beans to check). Appreciated! – Tiago Oliveira Apr 28 '21 at 23:34
  • You are welcome. Hope you are doing well. – loyal Apr 30 '21 at 01:17