Search code examples
flutterclassdartprovider

This error keeps returning, using the Provider class: Tried to call Provider.of<dynamic>


Error: Tried to call Provider.of<dynamic>.
This is likely a mistake and is therefore
unsupported.

If you want to expose a variable that can be anything, consider changing
`dynamic` to `Object` instead.
'package:provider/src/provider.dart':
Failed assertion: line 307 pos 7: 'T != dynamic'

what does chanfing it from dynamic to object mean?

Code:

  body: SingleChildScrollView(
  child: Container(
    width: MediaQuery.of(context).size.width,
    child: Column(
      children: <Widget>[
        FutureBuilder(
          future: Provider.of(context).auth.getCurrentUser(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return displayUserInformation(context, snapshot);
            } else {
              return CircularProgressIndicator();
            }

Solution

  • You need to specify the type that will return the static method Provider.of<T>(context).

    Dynamic is not an option here ;-)

    The example given in the Flutter documentation is self explanatory

    ChangeNotifierProvider(
          create: (context) => CartModel(),
          child: MyApp(),
        ),
    
    ....
    
    Provider.of<CartModel>(context, listen: false).removeAll();