如何解决Flutter TypeAheadField 使用提供者模式
我是 flutter 的初学者,在我的项目中我使用 TypeAheadField 小部件作为自动完成功能,现在我在使用提供者模式时遇到了问题。
我的问题是 SuggestionsCallback 参数,它不允许我引用从提供者返回的列表。
如果我指定 Provider 的返回类型,如 Provider.of<Stations>(context);
它显示了一个错误指示:
不能将参数 'Stations' 分配给参数类型 'FutureOr
这是我的 TypeAheadField 小部件的代码。
Widget searchItineraryBarre() {
String selectedDeparture;
String selectedArrival;
final stationsDATA = Provider.of<Stations>(context);
final stations = stationsDATA;
return SafeArea(
child: Container(
padding: const EdgeInsets.all(15),child: Column(
mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [
Container(
height: 40,child: TypeAheadField<Station>(
debounceDuration: Duration(milliseconds: 500),hideSuggestionsOnKeyboardHide: true,textFieldConfiguration: TextFieldConfiguration(
style: TextStyle(
fontSize: 13.0,height: .2,color: Colors.black),controller: _departureFromCity,decoration: InputDecoration(
filled: true,fillColor: Color.fromRGBO(238,239,240,.9),prefixIcon: Icon(Icons.location_on),border: OutlineInputBorder(
borderRadius: BorderRadius.circular(7),),hintText: 'Departure From',suggestionsCallback: Stations,itemBuilder: (context,Station suggestion) {
final station = suggestion;
return ListTile(
leading: Icon(Icons.directions_bus_rounded),title: Text(
station.nom,style: TextStyle(fontSize: 12,subtitle: Text(
station.commune,style: TextStyle(fontSize: 11,color: Colors.black54),trailing: Text(
station.nom_ar,color: Colors.blue),isThreeLine: true,);
},noItemsFoundBuilder: (context) => Container(
height: 100,child: Center(
child: Text(
'No result found',style: TextStyle(fontSize: 14),onSuggestionSelected: (Station suggestion) {
selectedDeparture = suggestion.nom;
this._departureFromCity.text = suggestion.nom;
print(selectedDeparture);
},
这是我的提供者 API。
class Stations with ChangeNotifier {
List<Station> _items = [];
List<Station> get items {
return [..._items];
}
Future<List<Station>> getStationSuggestion(String query) async {
final url = Uri.parse('http://192.168.6.44:3000/recherche/arret');
final response = await http.get(url);
if (response.statusCode == 200) {
notifyListeners();
final List arret = json.decode(response.body);
return _items = arret.map((json) => Station.fromJson(json)).where((arr) {
final namestation = arr.nom.toLowerCase();
final queryname = query.toLowerCase();
return namestation.contains(queryname);
}).toList();
} else {
throw Exception();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。