Tab*_*ps 2 provider android ios android-studio flutter
我正在 Flutter 中开发一个应用程序。我尝试在应用程序中使用 Provider 但遇到了这个错误:
Tried to listen to a value exposed with provider, from outside of the widget tree.
E/flutter ( 2850):
E/flutter ( 2850): This is likely caused by an event handler (like a button's onPressed) that called
E/flutter ( 2850): Provider.of without passing `listen: false`.
E/flutter ( 2850):
E/flutter ( 2850): To fix, write:
E/flutter ( 2850): Provider.of<Complexes>(context, listen: false);
E/flutter ( 2850):
E/flutter ( 2850): It is unsupported because may pointlessly rebuild the widget associated to the
E/flutter ( 2850): event handler, when the widget tree doesn't care about the value.
Run Code Online (Sandbox Code Playgroud)
我的代码是:
Tried to listen to a value exposed with provider, from outside of the widget tree.
E/flutter ( 2850):
E/flutter ( 2850): This is likely caused by an event handler (like a button's onPressed) that called
E/flutter ( 2850): Provider.of without passing `listen: false`.
E/flutter ( 2850):
E/flutter ( 2850): To fix, write:
E/flutter ( 2850): Provider.of<Complexes>(context, listen: false);
E/flutter ( 2850):
E/flutter ( 2850): It is unsupported because may pointlessly rebuild the widget associated to the
E/flutter ( 2850): event handler, when the widget tree doesn't care about the value.
Run Code Online (Sandbox Code Playgroud)
设置为 时listen:false,问题解决。但我认为这不是解决这个问题的好方法。另外,我需要提供者倾听。我已经搜索了互联网,但找不到任何合适的解决方案。
这是我的 Flutter 医生:
[?] Flutter (Channel stable, v1.12.13+hotfix.7, on Microsoft Windows [Version 10.0.18363.657], locale en-US)
• Flutter version 1.12.13+hotfix.7 at C:\android\flutter
• Framework revision 9f5ff2306b (6 weeks ago), 2020-01-26 22:38:26 -0800
• Engine revision a67792536c
• Dart version 2.7.0
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:/android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 28.0.3
• ANDROID_HOME = C:/android/sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[?] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 43.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[?] IntelliJ IDEA Ultimate Edition (version 2019.3)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4
• Flutter plugin version 42.1.4
• Dart plugin version 193.5731
[!] VS Code (version 1.41.1)
• VS Code at C:\Users\Altay\AppData\Local\Programs\Microsoft VS Code
X Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[?] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 5.0.2 (API 21) (emulator)
! Doctor found issues in 1 category.
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
我的提供者块:
loadedComplexes = Provider.of<Complexes>(context, listen: true).favoriteItems;
Run Code Online (Sandbox Code Playgroud)
调用提供者的方法:
[?] Flutter (Channel stable, v1.12.13+hotfix.7, on Microsoft Windows [Version 10.0.18363.657], locale en-US)
• Flutter version 1.12.13+hotfix.7 at C:\android\flutter
• Framework revision 9f5ff2306b (6 weeks ago), 2020-01-26 22:38:26 -0800
• Engine revision a67792536c
• Dart version 2.7.0
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:/android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 28.0.3
• ANDROID_HOME = C:/android/sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[?] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 43.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[?] IntelliJ IDEA Ultimate Edition (version 2019.3)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4
• Flutter plugin version 42.1.4
• Dart plugin version 193.5731
[!] VS Code (version 1.41.1)
• VS Code at C:\Users\Altay\AppData\Local\Programs\Microsoft VS Code
X Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[?] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 5.0.2 (API 21) (emulator)
! Doctor found issues in 1 category.
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
如果有必要,我可以提供更多信息。这个问题有什么解决方案吗?
提前致谢。
该错误清楚地表明您将其声明在Widget树的外面。如果要设置listen为 true,则必须在Widget树中声明它,以便提供程序知道Widget发生更改时必须重建哪棵树。
例子
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
loadedComplexes = Provider.of<Complexes>(context, listen: true).favoriteItems;//You must declare here if listen to true
return Scaffold(
body: Center(
child: MyWidget(),
),
);
Run Code Online (Sandbox Code Playgroud)
} }
设置listen为true使整个小部件作为侦听器并导致重建整个小部件树。但是设置 listen tofalse将帮助您重建您声明为的特定小部件Consumer(消费者是一个监听更改的小部件)。
| 归档时间: |
|
| 查看次数: |
5003 次 |
| 最近记录: |