flutter - Flutter typeAhead错误:queryrow不是字符串的子类型
问题描述
我尝试使用预先输入在文本字段中显示建议,但我收到错误“queryro 不是字符串的子类型”
Future<List<dynamic>> _getSuggestions(String query) {
return OrderProvider.db.rawQuery(
"select TypeOfProduct from Items where TypeOfProduct = ?", [query]);
} **这是我用来从数据库中获取数据的代码**
TypeAheadFormField(
textFieldConfiguration: TextFieldConfiguration(
controller: this._typeController,
autocorrect: true,
autofocus: true,
decoration: InputDecoration(hintText: "Product Name"),
),
suggestionsCallback: (suggestion) async {
return _getSuggestions(suggestion);
},
itemBuilder: (context, suggestion) {
return ListTile(
title: Text(suggestion),
);
},
onSuggestionSelected: (suggestion) {
this._typeController.text = suggestion;
},
),
解决方案
OrderProvider.db.rawQuery(
"select TypeOfProduct from Items where TypeOfProduct = ?", [query]);
这将返回一个List<Map>
(如果您使用的是 sqfLite),suggestsCallback 会将该列表传递给 itemBuilder,但 itemBuilder 正在使用这些建议来构建一个 Text Widget(并且 Text 需要一些 type String
,而不是 type Map
)。_getSuggestions(String query)
因此您可以在方法中使用 return 来返回 List之前减少 Map或执行类似的操作
itemBuilder: (context, suggestion) {//suggestion is of type Map because thats what you returned
return ListTile(
title: Text(suggestion['product']), //or whatever the key name is
);
},