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:
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.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp widget class.
1 |
void main() => runApp(MyApp()); |
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.
1 2 3 4 5 6 7 |
abstract class Message{ void hello(); void by(); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Second extends Message { void hello(){ print('Hello Guys............'); } void by(){ print('By By Guys.............'); } } |
5. Create our main MyApp class extends with StatelessWidget. This is our main View widget class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
6. Create a Second class object in MyApp class. Using the object we would call Abstract class methods.
1 |
final second = new Second(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: RaisedButton( onPressed: () => second.hello(), child: Text('Call Abstract Class Hello Function', textAlign: TextAlign.center,), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(0, 10, 0, 0), child: RaisedButton( onPressed: () => second.by(), child: Text('Call Abstract Class By Function', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.cyan, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
8. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); abstract class Message{ void hello(); void by(); } class Second extends Message { void hello(){ print('Hello Guys............'); } void by(){ print('By By Guys.............'); } } class MyApp extends StatelessWidget { final second = new Second(); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(0, 0, 0, 0), child: RaisedButton( onPressed: () => second.hello(), child: Text('Call Abstract Class Hello Function', textAlign: TextAlign.center,), textColor: Colors.white, color: Colors.pink, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(0, 10, 0, 0), child: RaisedButton( onPressed: () => second.by(), child: Text('Call Abstract Class By Function', textAlign: TextAlign.center), textColor: Colors.white, color: Colors.cyan, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
Screenshots: