0

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. 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.

The code for my BuildResults when someone taps 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 parameter and work with them in a UI with Bloc?

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
lasd
  • 26
  • 4

2 Answers2

1

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.

Abhishek Doshi
  • 465
  • 2
  • 5
1

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));
},
avinash
  • 501
  • 2
  • 8
  • 16
  • Thanks for the answer man,this is exactly what I have done, I already had an idea of ​​being able to do this but I did not know if doing it would end in bad practices of the Bloc pattern – lasd Apr 01 '22 at 14:00