- 7 Tem 2013
- 8,251
- 726
Hello! In this topic, I will talk about how to use "super" and override methods in the Dart programming language.
For example, let's consider two classes: "UserCore.dart" and "UserViewModel.dart".
In the "UserCore.dart" file, we have the "BaseViewModel" class:
In "UserViewModel.dart", we have the "UserViewModel" class that extends the "BaseViewModel" class:
For example, let's consider two classes: "UserCore.dart" and "UserViewModel.dart".
In the "UserCore.dart" file, we have the "BaseViewModel" class:
Kod:
import 'package:flutter/material.dart';
class BaseViewModel {
late BuildContext context;
late String text;
late String name;
void setContext(BuildContext context) {
this.context = context;
}
BaseViewModel(this.context);
}
In "UserViewModel.dart", we have the "UserViewModel" class that extends the "BaseViewModel" class:
Kod:
import 'package:deneme8/core/usercore.dart';
import 'package:flutter/cupertino.dart';
class UserViewModel extends BaseViewModel{
UserViewModel(BuildContext context) : super(context);
// The "super" method sends the context to the UserViewModel's constructor method and also starts the BaseViewModel's constructor.
late String userName;
String get name=> super.name + userName;
// Here, we can access both "name" and "userName", where "name" is inherited from the "BaseViewModel" class.
@override
void setContext(BuildContext context) {
// TODO: implement setContext
// Here, we can override the "setContext" method which is coming from the "BaseViewModel" class.
super.setContext(context);
}
}