gpt4 book ai didi

android - 听 :true 时在 flutter 中使用 Provider 的问题

转载 作者:行者123 更新时间:2023-12-04 14:35:34 25 4
gpt4 key购买 nike

我正在开发 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.
我的代码是:
loadedComplexes = Provider.of<Complexes>(context, listen: true).favoriteItems;
何时设置为 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
我的提供商 block :
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => Salons(),
),
ChangeNotifierProvider(
create: (ctx) => Cities(),
),
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ChangeNotifierProvider(
create: (ctx) => Complexes(),
),
ChangeNotifierProvider(
create: (ctx) => UserInfo(),
),
ChangeNotifierProvider(
create: (ctx) => User(),
),
],
child: MaterialApp(........
调用提供者的方法:
Future<void> _submit() async {
loadedComplexes.clear();
loadedComplexes =
Provider.of<Complexes>(context, listen: true).favoriteItems;
loadedComplexestolist.addAll(loadedComplexes);
}
如果有必要,我可以提供更多信息。这个问题有什么解决办法吗?
提前致谢。

最佳答案

该错误清楚地表明您在 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(),
),
);
}
}
设置 listentrue将整个小部件作为监听器并导致重建整个小部件树。但是设置听 false将帮助您重建您声明为 Consumer 的特定小部件(消费者是一个监听变化的小部件)。
check this video mate. He uses the same approach as you did

关于android - 听 :true 时在 flutter 中使用 Provider 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com