GreenCodeSki
GreenCodeSki

Reputation: 909

The value of local variable isn't used

I am new to flutter and I was following a tutorial when this error popped up. The ancestorRenderObjectOfType has deprecated and been replaced by findAncestorRenderObjectOfType so dart is throwing me errors.

What the tutor did in his video of old dart:

static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<BlocProvider<T>>();
BlocProvider<T> provider = context
    .context.ancestorRenderObjectOfType(type); 
return provider.bloc;

}

static Type _typeOf() => T; }

What I did in my code

static T of<T extends BlocBase>(BuildContext context) {
    final type = _typeOf<BlocProvider<T>>();
    BlocProvider<T> provider = context
        .findAncestorRenderObjectOfType();
    return provider.bloc;
  }

  static Type _typeOf<T>() => T;
}

If I put

BlocProvider<T> provider = context
        .findAncestorRenderObjectOfType(type);

I get an error saying Too many positional arguements. 0 expected, but 1 found.

The ENTIRE code

abstract class BlocBase {
  void dispose();
}

//Genric Bloc provider
class BlocProvider<T extends BlocBase> extends StatefulWidget {
  BlocProvider({
    Key key,
    @required this.child,
    @required this.bloc,
  }) : super(key: key);

  final T bloc;
  final Widget child;

  @override
  _BlocProviderState<T> createState() => _BlocProviderState<T>();

  static T of<T extends BlocBase>(BuildContext context) {
    final type = _typeOf<BlocProvider<T>>();
    BlocProvider<T> provider = context
        .findAncestorRenderObjectOfType(); //context.ancestorRenderObjectOfType(type);  GOTCHA
    return provider.bloc;
  }

  static Type _typeOf<T>() => T;
}

class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
  @override
  void dispose() {
    widget.bloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

Thank you!

Upvotes: 0

Views: 1443

Answers (1)

Sisir
Sisir

Reputation: 5388

findAncestorRenderObjectOfType doesnt take the type as argument instead it is a generic method where you can provide the type while calling the method. So your code will be as below:

static T of<T>(BuildContext context) {
  BlocProvider<T> provider = context
    .findAncestorRenderObjectOfType<BlocProvider<T>>();
  return provider.bloc;
}

Upvotes: 1

Related Questions