如何在Flutter中处理ChangeNotifierProvider.value( )
所以不会有内存泄漏
我将作为ChangeNotifierProvider.value( )的值的对象作为单例对象,因为我需要多个页面才能在一个对象中共享它们的状态
发布于 2020-04-07 04:38:30
您需要使用State的dispose()方法或ChangeNotifierProvider的默认构造函数。后者自动释放在create函数中创建的对象。
我想知道为什么您要使用ChangeNotifierProvider.value()而不是ChangeNotifierProvider(),但是假设您想要将值传递给另一个页面,您可以将两者结合起来,如下所示。
ChangeNotifierProvider<Foo>(
create: (context) => Foo(), // this Foo object is disposed of automatically
)在树下的某处:
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context2) => Provider<Foo>.value(
value: Provider.of<Foo>(context, listen: false),
child: NextPage(),
),
),
)注意:如果您提供了上述MaterialApp中的值,则不必执行此操作。
https://stackoverflow.com/questions/61067947
复制相似问题