TransWikia.com

The value of local variable isn't used

Stack Overflow Asked by greenycodeskii on November 4, 2021

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!

One Answer

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

Answered by Sisir on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP