Flutter Interview Topics

# extends V/S implements V/S with

extends:

You use extend if you want to create a more specific version of a class. For instance, if Apple extends from the Fruit class, it means all the properties, variables, and functions defined in the Fruit class will be available in the Apple class.

class First { 
  static int num = 1;
  void firstFunc(){
    print('hello');
  }
}
 
// inherits from First class
class Second extends First{ 
//   No need to override
}

implements:

The implement keyword is used to implement an interface by forcing the redefinition of the functions.

// Class with name First
class First { 
   
  // function to print "hello"
  void firstFunc(){ 
    print('hello');
  }
}
 
// We inherit the propertied
// of implemented class
class Second implements First{
   
  // by overriding the functions
  // in implemented class
  @override
  void firstFunc(){
    print('We had to declare the methods of implemented class');
  }
}

with: 

With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.

// mixin with name First
mixin First {
  void firstFunc(){
    print('hello');
  }
}
 
// mixin with name temp
mixin temp { 
  void number(){
    print(10);
  }
}
 
// mixin type used with keyword
class Second with First, temp{ 
  @override
  void firstFunc(){
    print('can override if needed');
  }
}

# Mixins V/S Inheritance

Mixins

  • It’s a way to reuse code across multiple classes.
  • Applied using the “with” keyword followed by one or more mixin names.
  • Can be applied to multiple classes simultaneously.
  • Mixins don't have constructors.
  • Mixins can't be instantiated directly.

Inheritance

  • It’s a way to create a relationship between classes where one class inherits properties and methods from another.
  • Achieved by using the “extends” keyword to specify the superclass.
  • Can only inherit from one superclass at a time.
  • Subclasses inherit constructors from the superclass.
  • Subclasses can be instantiated directly.


# Use of  `WidgetsFlutterBinding.ensureInitialized();`

A simple answer is that if Flutter needs to call native code before calling runApp()

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

# Dart is single thread language for using multiple threads in Dart we need to use `Isolate()`

# Lifecycle of StatefulWidget()

#METHODDESCRIPTION
1createState()When the Framework is instructed to build a StatefulWidget, it immediately calls createState()
~mounted is trueWhen createState creates your state class, a buildContext is assigned to that statebuildContext is the place in the widget tree in which this widget is placed. All widgets have a bool this mounted property. It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.

2initState()This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must call super.initState().

3didChangeDependencies()This method is called immediately after initState on the first time the widget is built.

4build()This method is called often. It is required, and it must return a Widget.

5didUpdateWidget(Widget oldWidget)If the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it's being rebuilt with the same runtimeType, then this method is called. This is because Flutter is re-using the state, which is long-lived. In this case, you may want to initialize some data again, as you would in initState.

~setState()This method is called often from the framework itself and from the developer. It's used to notify the framework that data has changed.

6deactivate()deactivate() is called when the State object is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists because State objects can be moved from one point in a tree to another.

7dispose()dispose() is called when the State object is removed, which is permanent. This method is where you should unsubscribe and cancel all animations, streams, etc.

~mounted is falseThe state object can never remount, and an error will be thrown if setState is called.



Post a Comment

Previous Post Next Post

Subscribe Us


Get tutorials, Flutter news and other exclusive content delivered to your inbox. Join 1000+ growth-oriented Flutter developers subscribed to the newsletter

100% value, 0% spam. Unsubscribe anytime