'Another exception was thrown: type 'RxList<dynamic>' is not a subtype of type 'List<Vehicle>' in type cast
I want to implement search functionality from database but it shows type cast error
Another exception was thrown: type 'RxList<dynamic>' is not a subtype of type 'List<Vehicle>' in type cast
In my Vehicle_Controller class
final vehicleList = [].obs;
Future<void> getVehicles() async {
List<Map<String, dynamic>> vehicleDetails = await DBHelper.query();
vehicleList.assignAll(vehicleDetails.map((data) => Vehicle.fromJson(data)).toList());
}
I want to implement search in my search class
final _vehicleController = Get.put(VehicleController());
List<Vehicle> _list;
List<Vehicle> _searchList = List();
void initState() {
super.initState();
_IsSearching = false;
_list = _vehicleController.vehicleList as List<Vehicle>; //type cast error
_searchList = _list;
_searchQuery.addListener(() {
if (_searchQuery.text.isEmpty) {
setState(() {
_IsSearching = false;
_searchText = "";
_buildSearchList();
});
} else {
setState(() {
_IsSearching = true;
_searchText = _searchQuery.text;
_buildSearchList();
});
}
});
}
Solution 1:[1]
you should use _vehicleController.vehicleList.value as vehicleList is -like the exception says- a RxList not a List
also update this line to be a <Vehicle>[].
final vehicleList = <Vehicle>[].obs;
this will make vehicleList of type RxList<Vehicle>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
