【问题标题】:This error keeps returning, using the Provider class: Tried to call Provider.of<dynamic>此错误不断返回,使用 Provider 类:尝试调用 Provider.of<dynamic>
【发布时间】:2021-06-23 20:23:26
【问题描述】:
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'

将它从动态转换为对象是什么意思?

代码:

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

【问题讨论】:

    标签: flutter class dart provider


    【解决方案1】:

    您需要指定将返回静态方法Provider.of&lt;T&gt;(context) 的类型。

    动态在这里不是一个选项;-)

    Flutter 文档中给出的示例是不言自明的

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

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 2020-02-19
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多