简体   繁体   English

在有状态小部件中使用提供程序

[英]Using Provider in a Stateful Widget

I'm pretty new to Flutter/Firebase and am having an issue with Provider implenetation for a very simple CRUD contacts app.我是 Flutter/Firebase 的新手,并且在一个非常简单的 CRUD 联系人应用程序的 Provider 实现方面遇到了问题。 I'm not sure how to fix this.我不知道如何解决这个问题。

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: 'package:provider/src/provider.dart': Failed assertion: line 284 pos 7: 'T != dynamic': Tried to call Provider.of. [VERBOSE-2:ui_dart_state.cc(166)] 未处理的异常:'package:provider/src/provider.dart':断言失败:第 284 行 pos 7:'T != dynamic':试图调用 Provider.of。 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.如果您想公开一个可以是任何变量的变量,请考虑将dynamic改为Object

Here's the code for the page where the save is done.这是完成保存的页面的代码。

actions: [
        IconButton(
            icon: Icon(
              Icons.done,
              color: Colors.white,
            ),
            onPressed: () async {
              //save data to firebase
              final uid = await Provider.of<dynamic>(context, listen: false)
                  .auth
                  .getCurrentUID();

              await db
                  .collection("userData")
                  .doc(uid)
                  .collection('contacts')
                  .add(
                {
                  'Name': widget.contact.name,
                  'PhoneNumber': widget.contact.phoneNumber,
                  'Location': widget.contact.location,
                  'Notes': widget.contact.notes
                },
              );

Thank you谢谢

The error message is pretty self-explanatory.错误消息是不言自明的。

You need to use the correct type (or Object ) in the Provider.of statement instead of dynamic.您需要在 Provider.of 语句中使用正确的类型(或Object )而不是动态的。

await Provider.of<CorrectType>(context, listen: false)
  .auth
  .getCurrentUID();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Stateful Widget 上的提供程序 - 参数类型问题 - Provider on Stateful Widget - issue with parameter type 如何将 Stateful Widget 中的 initState 替换为 provider 模式并将 stateless 替换为 stateful - How to replace initState in Stateful Widget with provider mode and replace stateless to stateful Provider (ChangeNotifier) 小部件内的有状态小部件未更新 - Stateful widget inside an Provider (ChangeNotifier) widget does not get updated 无状态小部件到有状态小部件(使用创建状态) - Stateless widget to stateful widget ( using Create state) 不能在 showBotttomSheet 的有状态小部件内使用提供程序 - Provider can't be consumed inside stateful widget of showBotttomSheet 在状态控件的状态下创建一个对象,该对象依赖提供者 - Create an object in stateful widget's state that has a dependency on provider 如何更改 Stateful Widget 中 Switch 的 state,从 Provider 检索数据? - How to change the state of a Switch in a Stateful Widget, retrieving data from a Provider? 使用 Provider 是否意味着没有 Stateful Widgets? - Does using Provider mean no Stateful Widgets? 如何使用脚手架在有状态小部件中转换此代码? - How to convert this code in stateful widget using scaffold? 使用颤振有状态小部件解析数据并使用它 - parssing data with flutter stateful widget and using it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM