i have a flutter application with wordpress backend, the data is in english in the backend , when the user change the language in the app the data is translated using AWS ..
Now the search in arabic is not working and i want to translate the keyword entered by user to english so i can fetch data from backend ...
this is my SearchBloc.dart
import 'dart:async';
import 'dart:convert';
import 'package:bloc/bloc.dart';
import 'package:WenoApp/configs/config.dart';
import 'package:WenoApp/models/model.dart';
import 'package:WenoApp/repository/list_repository.dart';
import 'package:WenoApp/utils/utils.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:rxdart/rxdart.dart';
import 'bloc.dart';
class SearchBloc extends Bloc<SearchEvent, SearchState> {
SearchBloc() : super(InitialSearchState());
final ListRepository listRepository = ListRepository();
@override
Stream<Transition<SearchEvent, SearchState>> transformEvents(
Stream<SearchEvent> events,
TransitionFunction<SearchEvent, SearchState> transition) {
final nonDebounceStream = events.where((event) => event is! OnSearch);
final debounceStream = events
.where((event) => event is OnSearch)
.debounceTime(Duration(milliseconds: 1500));
return super.transformEvents(
MergeStream([nonDebounceStream, debounceStream]),
transition,
);
}
@override
Stream<SearchState> mapEventToState(SearchEvent event) async* {
if (event is OnSearch) {
if (event.keyword.isNotEmpty) {
yield SearchLoading();
///Fetch API
final ResultApiModel response = await listRepository.loadList(
{"s":Translate.of(context).translate(event.keyword),"per_page":50}
);
if (response.success) {
//final Iterable convertList = Translate.of(context).translate(response.data) ?? [];
final Iterable convertList = response.data ?? [];
final list = convertList.map((item) {
return ProductModel.fromJson(item);
}).toList();
yield SearchSuccess(list: list);
} else {
yield SearchFail();
}
}
}
if (event is OnLoadHistory) {
List<String> historyString = UtilPreferences.getStringList(
Preferences.search,
);
if (historyString != null) {
List<ProductModel> history;
try {
history = historyString.map((e) {
return ProductModel.fromJson(jsonDecode(e));
}).toList();
} catch (e) {
await UtilPreferences.remove(
Preferences.search,
);
history = [];
}
yield LoadingHistorySuccess(list: history);
} else {
yield LoadingHistorySuccess(list: []);
}
}
if (event is OnSaveHistory) {
List<String> historyString = UtilPreferences.getStringList(
Preferences.search,
);
if (historyString != null) {
if (!historyString.contains(jsonEncode(event.item.toJson()))) {
historyString.add(jsonEncode(event.item.toJson()));
await UtilPreferences.setStringList(
Preferences.search,
historyString,
);
}
} else {
await UtilPreferences.setStringList(
Preferences.search,
[jsonEncode(event.item.toJson())],
);
}
yield SaveHistorySuccess();
}
if (event is OnClearHistory) {
if (event.item == null) {
await UtilPreferences.remove(
Preferences.search,
);
} else {
List<String> historyString = UtilPreferences.getStringList(
Preferences.search,
);
historyString.remove(jsonEncode(event.item.toJson()));
await UtilPreferences.setStringList(
Preferences.search,
historyString,
);
}
yield RemoveHistorySuccess();
}
}
}
No when i use this line :
final ResultApiModel response = await listRepository.loadList(
{"s":Translate.of(context).translate(event.keyword),"per_page":50}
);
it gives me the error :
Failed assertion: line 446 pos 12: 'context != null': is not true.
so how can i use " Context " in the bloc page ?
question from:
https://stackoverflow.com/questions/65874200/failed-assertion-line-446-pos-12-context-null-is-not-true 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…