Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'. After migrating to Null Safety
Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'. After migrating to Null Safety
我正在将一个大型项目迁移到 null 安全,我遇到了一个奇怪的错误,我不确定如何修复。
“错误:类型参数 'T' 不符合 'GetIt.call' 上类型变量 'T' 的绑定 'Object'。”
class BaseView<T extends BaseProvider?> extends StatefulWidget {
final Widget Function(BuildContext context, T value, Widget? child)? builder;
final Function(T)? onModelReady;
BaseView({this.builder, this.onModelReady});
@override
_BaseViewState<T> createState() => _BaseViewState<T>();
}
class _BaseViewState<T extends BaseProvider?> extends State<BaseView<T?>> {
T model = locator<T>(); <---- This is throwing it
@override
void initState() {
if (widget.onModelReady != null) {
widget.onModelReady!(model);
}
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<T?>(
create: (context) => model,
child: Consumer<T>(builder: widget.builder!),
);
}
}
我找不到关于此错误的更多信息,到目前为止我尝试过的任何方法都没有奏效。有人可以帮忙吗?
我正在使用 Provider 进行状态管理,而 BaseView 是在构建过程中包装我所有其他视图的东西;例如:
class EquipmentMainView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BaseView<EquipmentProvider>(
onModelReady: (model) async {
model.getAllFunctions();
},..
为将来可能最终 运行 遇到此问题的任何其他人发帖,只是通过更改
更改了 jamesdlin 建议的 BaseProvider 的可空性
class BaseView<T extends BaseProvider?>
至
class BaseView<T extends BaseProvider>
当我升级到 flutter 2.0 时我遇到了类似的问题我在 Generic class 中创建了泛型方法来显式扩展 Base class Object 即
来自:
import 'package:get_it/get_it.dart';
class PoultryBabaRegistry<T> {
static GetIt _getIt = GetIt.instance;
static void register<T>(T model) {
_getIt.registerSingleton<T extends Object >(model, signalsReady: true);
}
static void remove<T>(T model) {
_getIt.unregister<T extends Object>(instance:model);
}
static T getIt<T>() {
return _getIt.get<T>();
}
}
至:
class PoultryBabaRegistry<T extends Object> {
static GetIt _getIt = GetIt.instance;
static void register<T extends Object>(T model) {
_getIt.registerSingleton<T >(model, signalsReady: true);
}
static void remove<T extends Object>(T model) {
_getIt.unregister<T>(instance:model);
}
static T getIt<T extends Object>() {
return _getIt.get<T>();
}
}
我正在将一个大型项目迁移到 null 安全,我遇到了一个奇怪的错误,我不确定如何修复。
“错误:类型参数 'T' 不符合 'GetIt.call' 上类型变量 'T' 的绑定 'Object'。”
class BaseView<T extends BaseProvider?> extends StatefulWidget {
final Widget Function(BuildContext context, T value, Widget? child)? builder;
final Function(T)? onModelReady;
BaseView({this.builder, this.onModelReady});
@override
_BaseViewState<T> createState() => _BaseViewState<T>();
}
class _BaseViewState<T extends BaseProvider?> extends State<BaseView<T?>> {
T model = locator<T>(); <---- This is throwing it
@override
void initState() {
if (widget.onModelReady != null) {
widget.onModelReady!(model);
}
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<T?>(
create: (context) => model,
child: Consumer<T>(builder: widget.builder!),
);
}
}
我找不到关于此错误的更多信息,到目前为止我尝试过的任何方法都没有奏效。有人可以帮忙吗?
我正在使用 Provider 进行状态管理,而 BaseView 是在构建过程中包装我所有其他视图的东西;例如:
class EquipmentMainView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BaseView<EquipmentProvider>(
onModelReady: (model) async {
model.getAllFunctions();
},..
为将来可能最终 运行 遇到此问题的任何其他人发帖,只是通过更改
更改了 jamesdlin 建议的 BaseProvider 的可空性class BaseView<T extends BaseProvider?>
至
class BaseView<T extends BaseProvider>
当我升级到 flutter 2.0 时我遇到了类似的问题我在 Generic class 中创建了泛型方法来显式扩展 Base class Object 即
来自:
import 'package:get_it/get_it.dart';
class PoultryBabaRegistry<T> {
static GetIt _getIt = GetIt.instance;
static void register<T>(T model) {
_getIt.registerSingleton<T extends Object >(model, signalsReady: true);
}
static void remove<T>(T model) {
_getIt.unregister<T extends Object>(instance:model);
}
static T getIt<T>() {
return _getIt.get<T>();
}
}
至:
class PoultryBabaRegistry<T extends Object> {
static GetIt _getIt = GetIt.instance;
static void register<T extends Object>(T model) {
_getIt.registerSingleton<T >(model, signalsReady: true);
}
static void remove<T extends Object>(T model) {
_getIt.unregister<T>(instance:model);
}
static T getIt<T extends Object>() {
return _getIt.get<T>();
}
}