[英]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.