Flutter Dart Abstract classes can’t be instantiated Try creating an instance of a subtype Error Solution

Abstract class has only function declaration not function body thought you cannot create its object directly. Because without function body we cannot write a set of code executes on function calling time. Abstract class are designed to hold only function declaration. Abstract classes works like a base of Sub class like skeleton in boys which holds all the function names before we start making them. This would make development easier for developer. So this is the reason Abstract class object directly cannot be created. So in this tutorial we would solve Flutter Dart Abstract classes can’t be instantiated Try creating an instance of a sub-type Error Solution.

How the error look like:

Flutter Dart Abstract classes can't be instantiated Try creating an instance of a subtype Error Solution

Contents in this project Flutter Dart Abstract classes can’t be instantiated Try creating an instance of a subtype Error Solution:

1. To solve this problem all we have to do is firstly crate abstract class than override the abstract class methods and then create sub class object and call abstract class methods using sub class object in main class. So open your project’s main.dart file and import material.dart package inside it.

2. Create void main runApp() method and here we would call our main MyApp widget class.

3. Create a Abstract class named as Message with 2 function declaration inside it. hello() and by(). As we all know already that we can only define function name in Abstract class.

4. Create another class named as Second and extends the abstract Message class init. Now we would override both abstract class methods in second class and define their body.

5. Create our main MyApp class extends with StatelessWidget. This is our main View widget class.

6. Create a Second class object in MyApp class. Using the object we would call Abstract class methods.

7. Creating Widget Build area in MyApp class. Now we would make 2 Raised Button in Center widget and call both Abstract class functions using Second class object. We are printing simple Console message on Terminal screen.

8. Complete source code for main.dart file:

Screenshots:

Flutter Dart Abstract classes can't be instantiated Try creating an instance of a subtype Error Solution Flutter Dart Abstract classes can't be instantiated Try creating an instance of a subtype Error Solution

Leave a Reply

Your email address will not be published. Required fields are marked *