gpt4 book ai didi

dart - Dart:使用Interface会产生错误: “isn' t的有效替代

转载 作者:行者123 更新时间:2023-12-03 04:54:50 27 4
gpt4 key购买 nike

我创建了一个小界面:

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);
}

然后我尝试实现这一点:

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;
}
}

但是该代码会产生以下错误:
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)

似乎Dart认为 BuildContext中的 IControllerdynamic,但显然并非如此。
我怎样才能解决这个问题?我是Dart的新手,不知道该怎么办。
感谢帮助 :)

最佳答案

我真笨。
我的进口声明有误。
线

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

产生此错误,因为文件夹 controller小写字母开头。

正确的导入语句是

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

但是无论我的愚蠢错误是什么,错误消息都非常有趣。

关于dart - Dart:使用Interface会产生错误: “isn' t的有效替代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60770258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com