Introduction of Classes in Flutter 3 ūüėú

Introduction

Hi folks, in this article, we learn about classes in a flutter. In my previous articles, I explained functions and some primary dart methodologies. If you are new to flutter I highly recommend reading my previous articles it is more helpful for you. So let’s get started.

Classes in Flutterūü§Ē

Classes are the main building blocks for object-oriented programming language. Class is a custom type of holding variables and some functions. If you are new to variables and functions please check my Variables Article and Functions Article

Here we take the bank example one person has an account name, bank name, account number and bank balance. We took this same example for our classes. Please see the below image. 

Classes in Flutter

void main() {
  final customer = Customer();
}

class Customer {
  String accountName;
  String bankName;
  int accountNumber;
  double bankBalance;
}

In the above image only we declare a class and create an instance of that class but we didn’t initialize them, so we need to initialize the declared bank variables.

Classes in Flutter

void main() {
  final customer = Customer(
      accountName: 'Savings Account',
      bankName: 'State Bank of India',
      accountNumber: 1234,
      bankBalance: 5000.00);
  print(customer.accountName);
}

class Customer {
  Customer(
      {required this.accountName,
      required this.bankName,
      required this.accountNumber,
      required this.bankBalance});
  final String accountName;
  final String bankName;
  final int accountNumber;
  final double bankBalance;
}

In the above image, I clearly show you how we can access a member class inside a class. So we need to know the dot(.) operator for accessing the member class as per the above image. Before that, we need to initialize using the constructor. And add some sample values in Person Class as per the above image.

Instance Methods

Instance Methods create functions inside a class. Because Please go to the class section. Class is a custom type holding a variable and functions. In this bank code in case, we need to get only the bank and account name we create a function for that because the function is a piece of code used for code reusability. For more details about the function Click HereNote: Any functions are inside a class called methods.

Instance Methods in Flutter

Conclusion

In this article, we learned about classes in a flutter.  But this is only the introduction section about classes. In future, we learn about Object Oriented in dart.  Please any doubts comment below. Thank you.

Similar Articles