fluttervariablesstateflutter-dependenciesflutter-animation

How to assign a property to a variable from outside


How to assign a property milks to a variable activeMilk from outside.

The instance member 'widget' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expressio. I Googled the information and can’t find it.

enter image description here


Solution

  • You can access it through widget.milk, but it can't be called in the initializer scope.

    So, override initState method and initialize it:

    void initState(){
    
     activeMilk = widget.milk;
     super.initState();
    
     }
    

    Hope it helps you.

    btw, (in questions and answers) it's preferable to provide the code as text not an image .