我刚刚将我的flutter_bloc库更新为6.1.1,其中声明:
不建议使用区块,不应使用。使用context.read或context.watch代替。将在7.0.0中删除。尝试用替换替换不推荐的成员的使用。
下面是我必须修改的代码的一部分:
class ContractSubscriptionForm extends StatelessWidget {
final ContractSubscription contractSubscription;
const ContractSubscriptionForm(this.contractSubscription, {Key ke
根据Flutter的文档和,据我所理解,提供者包的context.read<T>和context.watch<T>方法之间的一个关键区别在于触发小部件的重建。您可以在任何小部件的build方法中调用build来访问当前状态,并在状态更改时请求Flutter重新构建您的小部件。您不能在构建方法之外使用context.watch<T>(),因为这通常会导致微妙的bug。相反,他们说,使用context.read<T>(),它获得当前状态,但不要求颤振未来的重建。
我试着制作了这个简单的应用程序:
class MyDataNotifier extend
// Are these the same?
final model = Provider.of<Model>(context, listen: false);
final model = context.read<Model>();
// Are these the same?
final model = Provider.of<Model>(context);
final model = context.watch<Model>();
他们是一样的还是不一样的?如果它们是,那么当我在read方法中使用build()方法时,当Provid
我有一个自定义的带有页面PageView的BottomAppBar。当用户单击BottomAppBar中的项目时,我正在尝试更改页面。我正在使用Provider来管理全局状态。单击items会触发provider中的值更改,但PageView中没有在值更改时触发页面更改的方法。我如何才能做到这一点?下面是我的代码。
我的提供商:
class CurrentPage extends ChangeNotifier {
int _currentPage = 0;
int get currentPage => _currentPage;
setCurrentPage(int val
cart.dart代码
import 'package:flutter_bloc/flutter_bloc.dart';
abstract class CartEvent {}
class CartAdd extends CartEvent {
final int itemAdd;
CartAdd(this.itemAdd);
}
class CartRemove extends CartEvent {
final int itemRemove;
CartRemove(this.itemRemove);
}
class CartBloc extend
我想调用Navigator.of(context).pop()一次或几次,然后在导航完成后运行回调,但我很难找到一个简洁的解决方案。我编写了一个示例应用程序来说明我遇到的问题:
ProviderScreenA上的Screens、B和C都可以访问可空的value,可以将value设置为非空的value,要求value为非空,buildScreenC可以将value设置为null,并将其弹回ScreenA。
当您按ScreenC上的按钮返回到ScreenA时,它会成功地导航(应用程序不会崩溃),但是您会抛出一个Error,因为它试图在第一个pop之后构建ScreenB。
import 'd
我正在尝试使用riverpod在登录时存储Firestore中存储的用户信息。我想存储的两条信息是用户的手机号码和他订阅的帐户。如果这些字段为空,则用户将显示QR阅读器,该阅读器应读取此信息并将其存储为Firestore,并在本地更新riverpod
我的用户加载页面如下:我得到了错误setState() or markNeedsBuild() called during build.。为什么会这样呢?我认为这是因为代码在仍在构建的同时更新了build方法。但我对文档的理解是,context.read()不应该重新构建小部件。
final subscribedToProvider = Chan