代码之家  ›  专栏  ›  技术社区  ›  markhorrocks

将提供程序3转换为4后,应用程序崩溃

  •  0
  • markhorrocks  · 技术社区  · 5 年前

    Provider 4.0.1

    这是我试图转换的代码。我只是变了 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');
    

    对null调用了getter“language”。接收器:空

    Provider 3.0.3 但显然我需要做更多。

    this tutorial .

    编辑:我通过添加 lazy: false

    Future<String> translate(BuildContext context, _term) async {
    
      final String _languageCode = Provider.of<User>(context).language;
    

    发生异常。 _断言错误('package:provider/src/provider.dart':失败的断言:第213行位置7:'context.owner.debugBuilding ||听着== false | | | u debugIsInInheritedProviderUpdate':尝试侦听

    这可能是由事件处理程序(如按钮的onPressed)引起的 那叫Provider.of 不经过 listen: false

    要修复,请写入:Provider.of(上下文,听:假);

    它不受支持,因为它可能会毫无意义地重新生成小部件 当小部件树不关心时,与事件处理程序关联 关于价值。)

    我补充道 听着:假的 上面的行似乎已经解决了这个问题,但是我尝试使用的下一个提供程序产生了以下错误:

    试图从 小部件树。

    这可能是由事件处理程序(如按钮的onPressed)引起的 那叫Provider.of 不经过 听着:假的

    它不受支持,因为它可能会毫无意义地重新生成小部件 当小部件树不关心时,与事件处理程序关联 关于价值package:provider/src/provider.dart':失败 断言:第213行位置7:'context.owner.debugBuilding ||听着==

    我现在应该去调用提供者并添加 听着:假的 Provider . 我在代码中多次调用Provider,但最后一个错误没有返回代码位置。

    现在总是需要,当它不是以前或我错过了什么?我开始在每个实例化提供程序变量的调用中添加listen:false,它看起来很有效,但这是正确的方法吗?我应该补充一下吗 听着:假的 每一个电话 Provider.of 今天就到此为止?

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

    0 回复  |  直到 4 年前
        1
  •  8
  •   dukaric1991    5 年前

    我有同样的“问题”,如果我加上 listen: false 无论我打电话到哪里,问题都消失了,但我不知道这是否是正确的解决方案。。。?

        2
  •  6
  •   Aman kumar    4 年前

    在事件处理程序上,例如 按下 , OnTap公司 仅长按 我们必须使用

    Provider.of<T>(context,listen:false)
    

    原因是他们不会监听任何更新更改,而是负责进行更改。

    而文本等小部件则负责显示…因此每次更改都需要更新…因此使用

    Provider.of<T>(context,listen:true)  //by default is listen:true
    
        3
  •  2
  •   Khalil Khalil    5 年前

    listen : false

    更多信息,请阅读此文 go_to_link

        4
  •  2
  •   Jaime    5 年前

    listen:true 作为默认值是合乎逻辑的。

    它没有在不符合逻辑的事件处理程序中指定。 listen: false

    context.read<T>() // Provider.of<T>(context, listen: false)
    context.watch<T>() // Provider.of<T>(context)
    
        5
  •  1
  •   akash maurya    4 年前

    如果在生成外部使用提供程序 听着:假的

    然后,当然,您不能监听更改,因为它没有为更改再次构建小部件。这不是默认值,因为提供程序是 但是,如果在构建之外使用,则必须使用 听着:假的

        6
  •  1
  •   Gitesh kumar Jha    4 年前

    在我的情况下,我得到了以下信息error:-

    I/flutter ( 7206): Tried to listen to a value exposed with provider, from outside of the widget tree.
    I/flutter ( 7206): 
    I/flutter ( 7206): This is likely caused by an event handler (like a button's onPressed) that called
    I/flutter ( 7206): Provider.of without passing `listen: false`.
    I/flutter ( 7206): 
    I/flutter ( 7206): To fix, write:
    I/flutter ( 7206): Provider.of<AstroDetailsProvider>(context, listen: false);
    I/flutter ( 7206): 
    I/flutter ( 7206): It is unsupported because may pointlessly rebuild the widget associated to the
    I/flutter ( 7206): event handler, when the widget tree doesn't care about the value.
    

    如您所见,解决方案出现在错误消息本身中。

    或者,

    context.read<T>() is same as Provider.of<T>(context, listen: false) And
    context.watch<T>() is same as Provider.of<T>(context)```
    
    Ref :- https://github.com/rrousselGit/provider/issues/313
    
        7
  •  1
  •   Mrudul Addipalli    4 年前

     Provider.of<T>(context, listen: true).method(); 
    

    内部构建方法和

     notifyListeners();
    

    在该方法()中,它会导致无限递归,就像在每个

     notifyListeners(); 
    

     method(), 
    

    被反复调用并导致错误