# 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()
# | METHOD | DESCRIPTION |
---|---|---|
1 | createState() | When the Framework is instructed to build a StatefulWidget, it immediately calls createState()
|
~ | mounted is true | When createState creates your state class, a buildContext is assigned to that state. buildContext 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. |
2 | initState() | 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() . |
3 | didChangeDependencies() | This method is called immediately after initState on the first time the widget is built. |
4 | build() | This method is called often. It is required, and it must return a Widget. |
5 | didUpdateWidget(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. |
6 | deactivate() | 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. |
7 | dispose() | 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 false | The state object can never remount, and an error will be thrown if setState is called. |