使用上下文调用BlocProvider.of(),该上下文不包含WeatherBloc类型的Bloc/Cubit.<



我想添加"我的位置"搜索屏幕按钮。此按钮必须获得当前地理位置。但是出现了一些错误,按钮什么也没做,但是它显示在搜索屏幕上。

错误:使用上下文调用BlocProvider.of()时,上下文不包含WeatherBloc类型的Bloc/Cubit。

我代码:

class MySearchDelegate extends SearchDelegate {
String selectedResult;
final Function callback;
MySearchDelegate(this.callback);
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.my_location),
onPressed: () {
BlocProvider.of<WeatherBloc>(context)
.add(WeatherCurrentPositionRequested());
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
);
}
@override
Widget buildResults(BuildContext context) {
return Container(
child: Center(
child: Text(selectedResult),
),
);
}
@override
void showResults(BuildContext context) {
selectedResult = query;
callback(query);
close(context, query);
}
@override
Widget buildSuggestions(BuildContext context) {
List<String> searchResults =
[query].where((element) => element.contains(query)).toList();
return ListView.builder(
itemCount: searchResults.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(searchResults[index]),
onTap: () {
selectedResult = searchResults[index];
callback(selectedResult);
Navigator.pop(context);
},
);
},
);
}
}

集团代码:


class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
WeatherBloc() : super(null) {
add(WeatherCurrentPositionRequested());
}
@override
Stream<WeatherState> mapEventToState(WeatherEvent event) async* {
if (event is WeatherRequested) {
yield* _newWeatherRequested(event);
}
if (event is WeatherCurrentPositionRequested) {
yield* _newWeatherCurrentPositionRequested();
}
}
Stream<WeatherState> _newWeatherRequested(WeatherRequested event) async* {
yield WeatherLoadInProgress();
try {
final Weather weather = await WeatherService.fetchCurrentWeather(
query: event.city, lon: event.lon, lat: event.lat);
final List<Weather> hourlyWeather =
await WeatherService.fetchHourlyWeather(
query: event.city, lon: event.lon, lat: event.lat);
yield WeatherLoadSuccess(weather: weather, hourlyWeather: hourlyWeather);
} catch (_) {
yield WeatherLoadFailure();
}
}
Stream<WeatherState> _newWeatherCurrentPositionRequested() async* {
LocationPermission permission = await checkPermission();
if (permission == LocationPermission.whileInUse ||
permission == LocationPermission.always) {
Position lastKnownPosition = await getLastKnownPosition();
if (lastKnownPosition != null) {
add(WeatherRequested(
lat: lastKnownPosition.latitude.toString(),
lon: lastKnownPosition.longitude.toString()));
} else {
Position position =
await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
add(WeatherRequested(
lat: position.latitude.toString(),
lon: position.longitude.toString()));
}
} else {
await requestPermission();
add(WeatherCurrentPositionRequested());
}
}
}

主要。飞镖代码:

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => WeatherBloc(),
child: BlocBuilder<WeatherBloc, WeatherState>(
builder: (context, state) {
if (state is WeatherLoadSuccess) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Color.fromRGBO(71, 177, 230, 1),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: MySearchDelegate((query) {
BlocProvider.of<WeatherBloc>(context)
.add(WeatherRequested(city: query));
}));
},
)
],
),
body: Padding(
padding: EdgeInsets.only(top: 0),
child: MainScreenWrapper(
weather: state.weather, hourlyWeather: state.hourlyWeather),
),
);
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
),
);
}
}

您必须将BlocProvider.of<WeatherBloc>(context)作为参数传递给MySearchDelegate类,然后在按下按钮时使用它,或者您可以使用Builder Widget来使用上下文。

class MySearchDelegate extends SearchDelegate {

final wheatherBlockProvider

MySearchDelegate(this.callback,this.wheatherBlockProvider);

@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.my_location),
onPressed: () {

wheatherBlockProvider.add(WeatherCurrentPositionRequested());
},
),
];
}
}


IconButton(icon: Icon(Icons.search),
onPressed: () {
final weatherBlocProvier = BlocProvider.of<WeatherBloc>(context)
showSearch(
context: context,
delegate: MySearchDelegate((query) {
weatherBlocProvier.add(WeatherRequested(city:query));
},weatherBlocProvier));
},
)

最新更新