skip to Main Content

I have an abstract class A:

abstract class A {
    Future<String> firstMethod();
}

and I implemented this abstract class:

class Aimp implements A {
  @override
  Future<String> firstMethod() async {
      return "test";
  }
}

I have created another abstract class:

abstract class B extends A {
  
    Future<String> secondMethod();
}

and I implemented this abstract class:

class Bweb extends B {
    @override
      Future<Object> secondMethod() async {
          final t = //I want to call firstMethod()
         if(t.isNotEmpty()) // do sth
    }
}

In the implementation of secondMethod(), how can I call the implementation of firstMethod()?

I don’t want to use mixin.

2

Answers


  1. I try to use with instead:

    abstract class A {
        Future<String> firstMethod();
    }
    
    class Aimp implements A {
      @override
      Future<String> firstMethod() async {
          return "test";
      }
    }
    
    abstract class B with Aimp {
        Future<String> secondMethod();
    }
    
    class Bweb extends B {
      
        @override
        Future<String> secondMethod() async {
             final String t = await firstMethod(); //Your firstMethod function
             if(t.isNotEmpty) {
               return t;
             }
          return '';
        }
    }
    
    Login or Signup to reply.
  2. Then you need an object of Aimp class item as a field of Bweb class. Or place A class as a field for the B one.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search