简体   繁体   English

如何使用 BLoC Flutter 在小部件中使用实体(模型)

[英]How to consume the entitie (model) in a widget with BLoC Flutter

I am working on an application that uses BLoC for state management, this is the situation: I have a SearchDelegate that has a model as DataType ( SearchDelegate<SearchModel> ), this SearchDelegate consumes an API to get SearchResults.我正在开发一个使用 BLoC 进行 state 管理的应用程序,情况是这样的:我有一个 SearchDelegate,其数据类型为 model ( SearchDelegate<SearchModel> ),此 SearchDelegate 使用 API 来获取 SearchResults。 When I onTap() in one of the results I take the information in a variable with the name of searchResults after this I use close(context, result) and navigate to the new page, in this page I want to use all the information from that model, the problem is that I don't know how to do that.当我在其中一个结果中使用onTap()时,我将信息保存在一个名为searchResults的变量中,之后我使用close(context, result)并导航到新页面,在这个页面中我想使用来自model,问题是我不知道该怎么做。

The code for my BuildResults when someone taps in one of the options当有人点击其中一个选项时我的 BuildResults 的代码

onTap: () {
 final searchResult = SearchModel(
  position: LatLng( search.lat[0], search.lng[1]),
  name: search.text,
  description: search.placeName
 );
close(context, result);
pushToPage(context, const RoutePage());
},

How can I pass this parameter and work with them in a UI with Bloc?我如何传递此参数并在带有 Bloc 的 UI 中使用它们?

Actually, you can move this entire model creation part to the bloc and then pass the model to your state class. Once you have the model in the state class, you can access it through Bloc anywhere in the widget tree.实际上,您可以将整个 model 创建部分移动到 bloc,然后将 model 传递给您的 state class。一旦您在 state 883981818188 中拥有 model,您就可以通过 Bloc 树中的任何位置访问它。

you can make a required parameter in your RoutePage widget and then while navigating to the RoutePage screen you can pass the same您可以在 RoutePage 小部件中创建一个必需的参数,然后在导航到 RoutePage 屏幕时您可以传递相同的参数

// RoutePage widget class constructor 
const RoutePage({Key? key, required this.subjectModel})
      : super(key: key);
  final SearchModel searchModel;
// while navigation
onTap: () {
 final searchResult = SearchModel(
  position: LatLng( search.lat[0], search.lng[1]),
  name: search.text,
  description: search.placeName
 );
close(context, result);
pushToPage(context,   RoutePage(searchModel:searchResult));
},

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM