简体   繁体   English

如何在flutter provider包中正确使用消费者

[英]how to use consumer correctly in flutter provider package

This is the error message这是错误信息

: The following NoSuchMethodError was thrown building Consumer<PageOffsetNotifier>(dirty,
: dependencies: [_InheritedProviderScope<PageOffsetNotifier>]):
: The method 'unary-' was called on null.
: Receiver: null
: Tried calling: unary-()

this is the class with consumer这是有消费者的班级

class LeopardImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<PageOffsetNotifier>(
      builder: (context,notifier,child) => Positioned(
          width:300, left: -notifier.offset,
          child: child,
      ),
      child: Image.asset('assets/leopard.png'),
    );
  }
}

here PageOffsetNotifier is the class which extends with ChangeNotifier and passed on to ChangeNotifierProvider这里PageOffsetNotifier是扩展ChangeNotifier并传递给ChangeNotifierProvider 的类

the notifier will detect the change in offset value while swiping one page to another and perform the desired function.....通知程序将检测偏移值的变化,同时将一页滑到另一页并执行所需的功能.....

Please tell me what I'm missing or done wrong in the consumer , as a result, I'm getting this error请告诉我我在消费者中遗漏了什么或做错了什么,结果,我收到了这个错误

To Pass the context from a page to another using provider.使用提供程序将上下文从一个页面传递到另一个页面。 Then it's always wise to use its object那么使用它的对象总是明智的

ChangeNotifierProvider.value(
    value: object from Provider,
    child: the page you want to move
)

The parent widget of this will be :这个的父小部件将是:

ChangeNotiferProvider<ClassName>(
    
   oncreate: (context)=>ClassName(),
),

With this you can use consumer inside.有了这个,您可以在内部使用消费者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 Flutter 提供商 package 的消费者未更新 - Consumer from Flutter provider package not updating Flutter 如何使用提供程序与 get_it package - Flutter How to Use Provider with get_it package 在使用提供者 Package 的消费者中,我如何将图像设置为 Flutter 中的每个构建? - In the Consumer using the Provider Package how do I set the image to every build in Flutter? 何时使用 Provider.of<x> 与消费者<x>在 Flutter</x></x> - When to use Provider.of<X> vs. Consumer<X> in Flutter (Flutter) 如何正确使用 Consumer 和 ChangeNotifierProvider ? 找不到丢失的东西 - (Flutter) How to use Consumer and ChangeNotifierProvider correctly ? can't find what is missing 如何在 Flutter 中使用 Provider 正确获取 API - How to correctly fetch APIs using Provider in Flutter 在 Flutter 提供者如何检查消费者收到的数据何时更新 - in Flutter Provider How to check when data received in consumer is updated 如何在 Flutter 中使用另一个提供者内部的提供者 - How to use a provider inside of another provider in Flutter 怎么听:提供者 package 在 flutter 中的虚假作品? - How listen: false works in provider package in flutter? 如何在 statefulWidget 中使用颤振提供程序? - How to use flutter provider in a statefulWidget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM