How to use 'super' and override methods in Dart

Gauloran

Kıdemli Moderatör
7 Tem 2013
8,122
604
local
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:

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);
  }


}

q5yU9e.png
 

ABTOHAIM

Üye
27 Mar 2023
97
36
G:Sarajevo
Thank you for your topic, it is nice to see English topics. I dont have any knowledge about Dart but hope this will help others.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.