如果在集团建设者之外打电话,flutter bloc的状态不会更新
我在主文件中初始化了所有必要的集团。即使在此之后,如果从集体构建器外部调用,Bloc的状态也不会更新。
我正在尝试使用Bloc模式实现懒惰加载。
假定的操作是当ListView到达End Scroll Luctiler调用bloc函数时,该功能将获取下一个项目,并将最新项目以及以前的项目传递给ProductsLoadedState();
但是,即使从集团散发出状态后,Products LoadedState也不会被触发。
任何帮助都非常感谢。 谢谢。
我的集体代码是:
void _onLoadProducts(
LoadProductsPage event, Emitter<ProductsPageState> emit) async {
print("getting products");
page == 1
? emit(const ProductsPageLoadingState(isFirstFetch: true))
: emit(ProductsPageLoadingState(
oldProducts: this.products, isFirstFetch: false));
final List<ProductModel> products =
await productsRepository.handleGetSearchedProducts(
event.title,
event.category,
event.brand,
event.condition,
event.productCode,
page);
page++;
print("page number $page");
this.products.addAll(products);
print(this.products.length);
emit(ProductsPageLoadedState(searchedProducts: this.products));
}
main.dart文件:
final AppRouter _appRouter = AppRouter();
MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<ProductsPageBloc>(
create: (context) => ProductsPageBloc(),
),
],
child: MultiRepositoryProvider(
providers: [
RepositoryProvider(create: (context) => ProductsRepository()),
RepositoryProvider(create: (context) => AuthRepository()),
RepositoryProvider(create: (context) => BrandRepository()),
],
child: MaterialApp(
title: 'title',
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'T MS',
scaffoldBackgroundColor: const Color(0xffF6F6F9)),
onGenerateRoute: _appRouter.onGenerateRoute,
// home : AddressesPage(),
home: BlocProvider(
create: (context) => NetworkBloc(),
child: BlocBuilder<NetworkBloc, NetworkState>(
builder: (context, state) {
return UserMain();
},
),
),
),
));
}
}
products.dart文件:
class ProductsPage extends StatefulWidget {
final String title;
final String category;
final String brand;
final String condition;
final int pageNumber;
final String productCode;
const ProductsPage(
{Key key,
this.title,
this.category,
this.brand,
this.condition,
this.pageNumber,
this.productCode})
: super(key: key);
@override
_ProductsPageState createState() => _ProductsPageState();
}
class _ProductsPageState extends State<ProductsPage> {
String appBarTitle;
List<ProductModel> products = [];
bool isLoading = false;
final scrollController = ScrollController();
void setupScrollController(context) {
scrollController.addListener(() {
if (scrollController.position.atEdge) {
if (scrollController.position.pixels != 0) {
BlocProvider.of<ProductsPageBloc>(context).add(LoadProductsPage(
title: widget.title,
category: widget.category,
brand: widget.brand,
condition: widget.condition,
pageNumber: widget.pageNumber,
productCode: widget.productCode));
}
}
});
}
@override
void initState() {
setupScrollController(context);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(50),
child: CustomAppBar(
isSearchPage: true,
title: "tilte",
enableSearchBar: true,
),
),RepositoryProvider.of<ProductsRepository>(context)
body: BlocProvider(
create: (context) => ProductsPageBloc()
..add(
LoadProductsPage(
title: widget.title,
category: widget.category,
brand: widget.brand,
condition: widget.condition,
pageNumber: widget.pageNumber,
productCode: widget.productCode),
),
child: Scaffold(body: Center(
child: BlocBuilder<ProductsPageBloc, ProductsPageState>(
builder: (context, state) {
if (state is ProductsPageLoadingState) {
if (state.isFirstFetch) {
print("is first fetch");
return const LoadingSpinnerWidget();
} else {
print("is not first fetch${state.oldProducts}");
products = state.oldProducts;
}
} else if (state is ProductsPageLoadedState) {
isLoading = false;
print("is loaded ${state.searchedProducts} $isLoading");
products = state.searchedProducts;
print("state${state.searchedProducts.length}");
print("products${products.length}");
}
if (products.isNotEmpty) {
return ListView.builder(
controller: scrollController,
itemCount: products.length + (isLoading ? 1 : 0),
itemBuilder: (context, index) {
if (index < products.length) {
return ProductHorizontal(product: products[index]);
} else {
Timer(const Duration(milliseconds: 30), () {
scrollController.jumpTo(
scrollController.position.maxScrollExtent);
});
return const LoadingSpinnerWidget();
}
});
} else {
return const ShowCustomPage(
icon: CupertinoIcons.bag,
title:
"Sorry, we couldn't find any similar products. Please try searching for other products",
);
}
},
),
)),
),
);
}
}
I initialised all necessary blocs in my main file. Even after this, the state of bloc is not updating if called from outside the bloc builder.
I am trying to implement lazy loading using bloc pattern.
Supposed action is when listview reaches end scroll listener calls a bloc function which will fetch the next items and pass the latest item along with previous items to ProductsLoadedState();
But, ProductsLoadedState is not getting trigged even after emitting the state from bloc.
Any help is really appreciated.
Thank you.
My bloc code is :
void _onLoadProducts(
LoadProductsPage event, Emitter<ProductsPageState> emit) async {
print("getting products");
page == 1
? emit(const ProductsPageLoadingState(isFirstFetch: true))
: emit(ProductsPageLoadingState(
oldProducts: this.products, isFirstFetch: false));
final List<ProductModel> products =
await productsRepository.handleGetSearchedProducts(
event.title,
event.category,
event.brand,
event.condition,
event.productCode,
page);
page++;
print("page number $page");
this.products.addAll(products);
print(this.products.length);
emit(ProductsPageLoadedState(searchedProducts: this.products));
}
Main.dart file :
final AppRouter _appRouter = AppRouter();
MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<ProductsPageBloc>(
create: (context) => ProductsPageBloc(),
),
],
child: MultiRepositoryProvider(
providers: [
RepositoryProvider(create: (context) => ProductsRepository()),
RepositoryProvider(create: (context) => AuthRepository()),
RepositoryProvider(create: (context) => BrandRepository()),
],
child: MaterialApp(
title: 'title',
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'T MS',
scaffoldBackgroundColor: const Color(0xffF6F6F9)),
onGenerateRoute: _appRouter.onGenerateRoute,
// home : AddressesPage(),
home: BlocProvider(
create: (context) => NetworkBloc(),
child: BlocBuilder<NetworkBloc, NetworkState>(
builder: (context, state) {
return UserMain();
},
),
),
),
));
}
}
Products.dart file :
class ProductsPage extends StatefulWidget {
final String title;
final String category;
final String brand;
final String condition;
final int pageNumber;
final String productCode;
const ProductsPage(
{Key key,
this.title,
this.category,
this.brand,
this.condition,
this.pageNumber,
this.productCode})
: super(key: key);
@override
_ProductsPageState createState() => _ProductsPageState();
}
class _ProductsPageState extends State<ProductsPage> {
String appBarTitle;
List<ProductModel> products = [];
bool isLoading = false;
final scrollController = ScrollController();
void setupScrollController(context) {
scrollController.addListener(() {
if (scrollController.position.atEdge) {
if (scrollController.position.pixels != 0) {
BlocProvider.of<ProductsPageBloc>(context).add(LoadProductsPage(
title: widget.title,
category: widget.category,
brand: widget.brand,
condition: widget.condition,
pageNumber: widget.pageNumber,
productCode: widget.productCode));
}
}
});
}
@override
void initState() {
setupScrollController(context);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(50),
child: CustomAppBar(
isSearchPage: true,
title: "tilte",
enableSearchBar: true,
),
),RepositoryProvider.of<ProductsRepository>(context)
body: BlocProvider(
create: (context) => ProductsPageBloc()
..add(
LoadProductsPage(
title: widget.title,
category: widget.category,
brand: widget.brand,
condition: widget.condition,
pageNumber: widget.pageNumber,
productCode: widget.productCode),
),
child: Scaffold(body: Center(
child: BlocBuilder<ProductsPageBloc, ProductsPageState>(
builder: (context, state) {
if (state is ProductsPageLoadingState) {
if (state.isFirstFetch) {
print("is first fetch");
return const LoadingSpinnerWidget();
} else {
print("is not first fetch${state.oldProducts}");
products = state.oldProducts;
}
} else if (state is ProductsPageLoadedState) {
isLoading = false;
print("is loaded ${state.searchedProducts} $isLoading");
products = state.searchedProducts;
print("state${state.searchedProducts.length}");
print("products${products.length}");
}
if (products.isNotEmpty) {
return ListView.builder(
controller: scrollController,
itemCount: products.length + (isLoading ? 1 : 0),
itemBuilder: (context, index) {
if (index < products.length) {
return ProductHorizontal(product: products[index]);
} else {
Timer(const Duration(milliseconds: 30), () {
scrollController.jumpTo(
scrollController.position.maxScrollExtent);
});
return const LoadingSpinnerWidget();
}
});
} else {
return const ShowCustomPage(
icon: CupertinoIcons.bag,
title:
"Sorry, we couldn't find any similar products. Please try searching for other products",
);
}
},
),
)),
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您正在使用
productsPageBloc
的不同实例。通过事先初始化它,请确保使用相同的对象。例如:希望它有帮助。
It seems like you are using different instances of your
ProductsPageBloc
. Make sure you use the same object, by initializing it beforehand. For example:Hope it helps.