简体   繁体   中英

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. 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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM