简体   繁体   中英

Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'. After migrating to Null Safety

I'm in the process of migrating over a large project to null safety and I'm coming across a strange error I'm not entirely sure how to fix.

"Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'."

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

I can't find much info on this error and so far any method I've tried hasn't worked out. Can anyone be of assistance?

I'm using Provider for state management and BaseView is what wraps all my other views during build; eg:

class EquipmentMainView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BaseView<EquipmentProvider>(
      onModelReady: (model) async {
        model.getAllFunctions();
      },..

Posting here for anyone else that might eventually run across this in the future, just changed the nullability of BaseProvider suggested by jamesdlin by changing

class BaseView<T extends BaseProvider?>

to

class BaseView<T extends BaseProvider>

I had similar issues when I upgraded to flutter 2.0 I made the generic methods in Generic class to explicitly extends Base class Object ie

from:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM