Home > OS >  how can i listen to the bool variable and make an action
how can i listen to the bool variable and make an action

Time:02-01

i have StatefulWidget and local bool test = false so for example i need to make an action if its value has been changed to true ,,

class FollowingList extends StatefulWidget {
  final String uid ;
  const FollowingList({Key? key,required this.uid}) : super(key: key);

  @override
  _FollowingListState createState() => _FollowingListState();
}


class _FollowingListState extends State<FollowingList> {

  @override
  void initState() {
    super.initState();
    if(// here i need to tell dart wait till test is == true and if it is so .. make an action  ){
    //i need to make an action or call a function ,,
}
  }

bool test = false`

@override
  Widget build(BuildContext context) {
    return // of course i can use here onPressed then setstate test = true then make an action This does not fit my case 
   but i need auto detect  
     
  }
}

CodePudding user response:

Use ValueNotifier. Learn how to use

Step 1: Declare variable

ValueNotifier<int> buttonClickedTimes =ValueNotifier(0);

Step 2:Wrap it with builder

ValueListenableBuilder(
  valueListenable: buttonClickedTimes,
  builder: (BuildContext context, int counterValue,Widget child){
  ///This trigger every time the value of 'buttonClickedTimes' changes
  return Text("Counter:$counterValue");
      }
)

Step 3:Try to change value

GestureDetector(
  onTap:()=>buttonClickedTimes.value= buttonClickedTimes.value 1,
  child: Text("Button",style:Theme.of(context).textTheme.button)
);

CodePudding user response:

You could use ternary operations. So after waiting till test is true, set state. And then your code could be something like :

@override
void initState() {
super.initState();
if(// here i need to tell dart wait till test is == true and if it is so .. make an action  ){
 setState((){test = true;});
//i need to make an action or call a function ,,
}
}

Then :

@override
Widget build(BuildContext context) {
return test == true ? YourWidget() : OtherWidget(); 
 
}
}
  •  Tags:  
  • Related