我有一个带有 wordpress 后端的 flutter 应用程序,后端的数据是英文的,当用户更改应用程序中的语言时,数据将使用 AWS 进行翻译 ..
现在阿拉伯语搜索不起作用,我想将用户输入的关键字翻译成英文,以便我可以从后端获取数据...
这是我的 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();
}
}
}
不,当我使用这条线时:
final ResultApiModel response = await listRepository.loadList(
{"s":Translate.of(context).translate(event.keyword),"per_page":50}
);
它给了我错误: 失败的断言:第 446 行 pos 12:'context != null':不正确。
那么我如何在 bloc 页面中使用“上下文”?