Flutter dart call super of super class method

487

Since no one answered, here is how I solved this. I think this is impossible to achieve. What I did is I moved the code and all the variables to a mixin. Took like an hour or two to do so. I achieved my needs tho.

Share:
487
cs guy
Author by

cs guy

Just a random human trying to master cs

Updated on December 26, 2022

Comments

  • cs guy
    cs guy over 1 year

    Here is my architecture, I have two statfull widgets: ProfilePicture, RoundedProfilePicture. ProfilePicture is a statefull widget and it has complicated custom code which is very prone to error so I dont want to copy paste it, it utilizes variables from Picture abstract class. (ie. fetches server data and stores them inside variables of Picture). What I want to do is I want to extend from this widgets state so that I can create a new widget RoundedProfilePicture. This widgets state will basically utilize the complicated code from ProfilePicture's state with inheritance and it will add small extra logic to it. So I thought inheritance is the best choice here. Here is what I tried so far

    class ProfilePictureState extends State<ProfilePicture> implements Picture{
        // SOME LONG COMPLICATED 500 LINE OF CODE
    }
    
    class RoundedProfilePictureState extends ProfilePictureState {
        @override
        void initState() {
           super.initState(); // this calls ProfilePictureState.initState() call. I want to call State<ProfilePicture>.initState()
        }
     }
    

    My problem is void initState() in RoundedProfilePictureState requires me to make a super.initState() call. This call makes a ProfilePictureState.initState() call. I want to call State<ProfilePicture>.initState() because I want to add a different logic in my init state call. So the structure is:

    ----abstract State class
    ---ProfilePictureState 
    --RoundedProfilePictureState  
    

    How can I make a call to abstract State class's initState method from RoundedProfilePictureState? Is this possible?

    • Randal Schwartz
      Randal Schwartz over 3 years
      If you have to call super-super, the hierarchy is mis-designed. calling super means "I'm just like my parent class, except I add this additional behavior". If you ever need to ignore parent class, and skip to grandparent class, something is very wrong.
    • cs guy
      cs guy over 3 years
      Agreed. My hierarchy design was flawed