Home > Net >  How to consume the entitie (model) in a widget with BLoC Flutter
How to consume the entitie (model) in a widget with BLoC Flutter

Time:04-01

I am working in an application that uses BLoC for stateManagement, this is the situation: I have a SearchDelegate that has a model as DataType (SearchDelegate<SearchModel>), this SearchDelegate consumes an API to get 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 use all the information from that model, the problem is that I dont know how to do that.

The code for my BuildResults when someone tap in one of the options

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 parameters and work with them in an UI with Bloc?

CodePudding user response:

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.

CodePudding user response:

you can make a required parameter in your RoutePage widget and then while navigating to the RoutePage screen you can pass the same

// 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));
},
  • Related