Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document WidgetsFlutterBinding.ensureInitialized for early plugin calls #77745

Closed
dgaedcke opened this issue Mar 9, 2021 · 8 comments
Closed
Labels
d: api docs Issues with https://api.flutter.dev/ P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels.

Comments

@dgaedcke
Copy link

dgaedcke commented Mar 9, 2021

Certain plugins (eg await Firebase.initializeApp();) CAN be called BEFORE runApp() ... at top of:
void main() async {}

others (await SharedPreferences.getInstance(); ) MUST be called from a Widget INSIDE of runApp()

This was confusing to me and has not been clearly stated in any docs I've been able to find.

I suspect a lot of the people seeing "shared_preferences -- No implementation found for method getAll" were making the same mistake as I was ... trying to bind SharedPreferences too early in the load sequence.

@pedromassangocode pedromassangocode added the in triage Presently being triaged by the triage team label Mar 10, 2021
@pedromassangocode
Copy link

others (await SharedPreferences.getInstance(); ) MUST be called from a Widget INSIDE of runApp()

This is not true for all the cases, this is only required if your uses shared_preferences is needed before runApp().

I suspect a lot of the people seeing "shared_preferences -- No implementation found for method getAll" were making the same mistake as I was ... trying to bind SharedPreferences too early in the load sequence.

From what I know this kind of issue is mostly related to Proguard configurations (and sometimes Gradle version).

Certain plugins (eg await Firebase.initializeApp();) CAN be called BEFORE runApp() ... at top of:
void main() async {}

Can you please elaborate how you came to this conclusion?

@pedromassangocode pedromassangocode added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 10, 2021
@dgaedcke
Copy link
Author

You are 100% right .... I concluded this without exhaustive testing ... I did follow instructions with ProGuard and that did not fix my "No implementation found for method getAll" .... but when I moved the init of SharedPreferences down into the widget, I finally (after days of experimenting) got past this PITA error.

And if I count all the weeks I relied on "SharedPreferences.setMockInitialValues()" BEFORE Flutter 2.0 and null safety, I've had this error pestering me for months

I would love to see some clear specs on configuring Proguard and setting up Gradle ... thanks for the info!!

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 10, 2021
@pedromassangocode
Copy link

pedromassangocode commented Mar 11, 2021

I would love to see some clear specs on configuring Proguard and setting up Gradle ... thanks for the info!!

While I agree that this should be done, notice that this may not be the solution for everyone, as an example, this may be caused by how the plugins are being registered (#75416).

For the Firebase plugin request, please file a new issue in its repository: https://github.com/FirebaseExtended/flutterfire/issues

@pedromassangocode pedromassangocode added d: api docs Issues with https://api.flutter.dev/ p: first party p: shared_preferences Plugin to read and write Shared Preferences and removed in triage Presently being triaged by the triage team labels Mar 11, 2021
@pedromassangocode pedromassangocode changed the title Documentation suggestion/request [shared_preferences] Documentation suggestion/request Mar 11, 2021
@dgaedcke
Copy link
Author

You prompted me to dig more deeply and it turned out that our problem was not with SharePreferences at all ... that was just the error we were seeing.

It was actually caused by plugin "flutter_facebook_auth" failing to register upstream and leaving Flutter's plugin infrastructure in a confused state, so that it was reporting the error with the next plugin to come along.

Bogus error about SharePreferences sent us on a totally irrelevant wild goose chase!!

We don't have any current problems with Firebase .... it seems to be working fine.
Thanks for your response and for pushing me to get clear on what's really going on!!

@cyanglaz cyanglaz added the P3 Issues that are less important to the Flutter project label Mar 11, 2021
@stuartmorgan
Copy link
Contributor

We should ensure that WidgetsFlutterBinding.ensureInitialized is documented somewhere for the case of calling plugins early.

@stuartmorgan stuartmorgan removed p: first party p: shared_preferences Plugin to read and write Shared Preferences labels Mar 11, 2021
@stuartmorgan stuartmorgan changed the title [shared_preferences] Documentation suggestion/request Document WidgetsFlutterBinding.ensureInitialized for early plugin calls Mar 11, 2021
@Sameerkash
Copy link
Contributor

@stuartmorgan should this be documented while calling SharedPreferences.getInstance() ?

@stuartmorgan
Copy link
Contributor

Closing as a duplicate of #78028 which has more detail.

@github-actions
Copy link

github-actions bot commented Aug 2, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
d: api docs Issues with https://api.flutter.dev/ P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels.
Projects
None yet
Development

No branches or pull requests

5 participants