DarkMath
DarkMath

Reputation: 1419

Dart: Using Interface gives the error: "isn't a valid override of"

I've created a small interface:

import ...

abstract class IController {
  void navigateTo(BuildContext context, String routeName);
  Future<LocationData> get location;

  // registration process
  void registerGender(Gender gender);
  void registerBirthday(DateTime birthday);
  Future<bool> register(String email, String password);
}

And then I tried to implement this:

import ...

class Controller implements IController {
  static final Controller _instance = Controller._internal();
  final ServiceAuthenticate _serviceAuth = ServiceAuthenticate();
  final ServiceDatabase _serviceDb = ServiceDatabase();
  final ServiceGPS _serviceGPS = ServiceGPS();
  User _user;
  String _routeName;
  UserData _userData;

  Controller._internal() {
    this._routeName = ROUTE_WELCOME;
  }

  factory Controller() => _instance;

  void navigateTo(BuildContext context, String routeName) {
    this._routeName = routeName;
    Navigator.pushReplacementNamed(context, routeName);
  }

  Future<LocationData> get location async{
    this._userData.location = await this._serviceGPS.location;
    print(this._userData.location);
    return this._userData.location;
  }

  void registerGender(Gender gender){
    this._userData = UserData();
    this._userData.gender = gender;
  }

  void registerBirthday(DateTime birthday) {
    this._userData.birthday = birthday;
  }

  Future<bool> register(String email, String password) async {
    User user = await this._serviceAuth.registerWithEmailAndPassword(email, password);
    if(user == null){
      return false;
    }
    this._user = user;
    return true;
  }
}

But that code produces the following error:

error: 'Controller.navigateTo' ('void Function(BuildContext, String)') isn't a valid override of 'IController.navigateTo' ('void Function(dynamic, String)'). (invalid_override at [prototype] lib\controller\controller.dart:30)

It looks like Dart thinks, that the BuildContext in the IController is dynamic, but this is obviously not the case. How can I fix this? I'm new to Dart and don't know what to do. Thanks for help :)

Upvotes: 2

Views: 5426

Answers (3)

Akshay Chopra
Akshay Chopra

Reputation: 1253

The one line answer is : Your import statement is wrong.

But now , you need to take care in which file the import statement is going wrong.
There can be many scenarios, but I would like to give an example where I was stuck.

I had two different files in different package, but both files were importing some method where the method names were same.

So while importing the file which contain this method, I had imported the same name method from one file, and at other place, the same name method from second file.

So that's where everything went wrong!

So if import file is correct in the file which is giving some error, check the other dependent file, where same method import statement is written, that may be wrong.

Upvotes: 1

kynnysmatto
kynnysmatto

Reputation: 3892

A had a similar error and in my case the problem was that the return type of the buggy function was such that there were two different classes in the codebase with the same name. And the interface was using one and the implementation the other.

Upvotes: 1

DarkMath
DarkMath

Reputation: 1419

I'm stupid. My import statement was wrong. The line

import 'package:prototype/Controller/IController.dart';

produced this error, because the folder controller starts with a lowercase Letter.

The correct import statement is

import 'package:prototype/controller/IController.dart';

But regardless of my stupid mistake is the error message quite interesting.

Upvotes: 2

Related Questions