gpt4 book ai didi

将 Provider 3 转换为 4 后 Flutter 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 21:28:28 25 4
gpt4 key购买 nike

我今天尝试升级我的 Flutter 应用以使用 Provider 4.0.1,但以下代码在将值赋给 null 时崩溃了。

这是我尝试转换的代码。我只是将 SingleChildCloneableWidget 更改为 SingleChildStatelessWidget ,编译正常。

import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

List<SingleChildStatelessWidget> providers = [
...independentServices,
...dependentServices,
...uiConsumableProviders
];

List<SingleChildStatelessWidget> independentServices = [
Provider.value(value: Api()),
Provider.value(value: Tbl()),
Provider.value(value: Bill()),
Provider.value(value: Sale()),
Provider.value(value: Category()),
Provider.value(value: Menu()),
];

List<SingleChildStatelessWidget> dependentServices = [
ProxyProvider<Api, AuthenticationService>(
update: (context, api, authenticationService) => AuthenticationService(api: api),
),
];

List<SingleChildStatelessWidget> uiConsumableProviders = [
StreamProvider<User>(
create: (context) => Provider.of<AuthenticationService>(context, listen: false).user,
),
lazy: false
];

我是这样实现的:

StreamController<User> _userController = StreamController<User>();
Stream<User> get user => _userController.stream;

崩溃发生在这一行:

Future<void> _setFixedLanguageStrings(BuildContext context) async {

User _user = Provider.of<User>(context);

_user.homeString = await translate(context, 'Home');

The getter 'language' was called on null. Receiver: null

这在 Provider 3.0.3 上运行良好,但显然我需要做更多。

我的原始代码来自this tutorial .

编辑:我通过在流提供程序创建方法中添加 lazy: false 解决了该问题,但随后在此代码中出现了另一个错误。

Future<String> translate(BuildContext context, _term) async {

final String _languageCode = Provider.of<User>(context).language;

产生此错误:

Exception has occurred._AssertionError ('package:provider/src/provider.dart': Failed assertion: line 213 pos 7: 'context.owner.debugBuilding || listen ==false || _debugIsInInheritedProviderUpdate': Tried to listen to avalue exposed with provider, from outside of the widget tree.

This is likely caused by an event handler (like a button's onPressed)that called Provider.of without passing listen: false.

To fix, write: Provider.of(context, listen: false);

It is unsupported because may pointlessly rebuild the widgetassociated to the event handler, when the widget tree doesn't careabout the value. )

我在上面的行中添加了 listen: false ,这似乎解决了该问题,但是我尝试使用的下一个提供程序产生了此错误:

Tried to listen to a value exposed with provider, from outside of thewidget tree.

This is likely caused by an event handler (like a button's onPressed)that called Provider.of without passing listen: false.

To fix, write: Provider.of(context, listen: false);

It is unsupported because may pointlessly rebuild the widgetassociated to the event handler, when the widget tree doesn't careabout the value. 'package:provider/src/provider.dart': Failedassertion: line 213 pos 7: 'context.owner.debugBuilding || listen ==false || _debugIsInInheritedProviderUpdate'

我现在应该转到调用提供商的每个实例并添加 listen: false 吗?我需要有人解释一下发生了什么变化以及为什么发生了变化,因为我对 Flutter 还很陌生,而且 Provider 的文档很少。有很多次我在代码中调用 Provider,而最后一个错误没有返回代码位置。

现在是否总是需要 listen: false 而以前不需要,或者我是否错过了其他内容?我开始在每次调用中添加listen: false来实例化Provider变量,它似乎正在工作,但这是正确的方法吗?我是否应该在每次调用 Provider.of 时添加 listen: false 就到此为止?

编辑:每当从小部件树的可见部分外部调用提供程序时,就会出现错误。这种区别很重要。

最佳答案

我有同样的“问题”,如果我在调用 Provider 的所有地方添加 listen: false 问题就消失了,但我不知道这是否是正确的解决方案......?

关于将 Provider 3 转换为 4 后 Flutter 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590673/

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