Search code examples
flutterdartflutter-provider

how to pass provider classname as an argument?


I want to pass provider classname as an argument but it keeps giving me error The name 'providerClassName' isn't a type so it can't be used as a type argument.

I am trying this

  Widget buildText<T>({@required T providerClassName}) {
    return Text(
        Provider.of<providerClassName>(context, listen: false).username);
  }

and i am calling function like this

 buildText(providerClassName: HomeScreenProvider);

Solution

  • You almost got it correct. You have the generic type T of the method

    buildText<T extends HomeScreenProvider>
    

    so you can use it like Provider.of<T>(context, listen: false).username and call it
    buildText<HomeScreenProvider>();