Why am I getting a _TypeError when trying to create a list? - flutter

I have the following JSON that is getting returned from an API call:
{
"categories": {
"mortgage": "Mortgage",
"haircutsClothing": "Haircuts & Clothing",
"homeRepairMaintenance": "Home Repair & Maintenance"
},
"other": {...}
}
And then I have this class acting as a model for the JSON data:
class APIData {
final Map<String, dynamic> parsedJson;
APIData({required this.parsedJson});
factory APIData.fromJson(Map<String, dynamic> parsedJson) {
List<Category> categories = parsedJson['categories']
.map((i) => Category.fromJson(i))
.toList();
return APIData(parsedJson: parsedJson);
}
}
class Category {
final String key;
final String category;
Category({required this.key, required this.category});
factory Category.fromJson(Map<String, dynamic> parsedJson) {
return Category(key: parsedJson['key'], category: parsedJson['value']);
}
}
When I run that, I get this error:
_TypeError (type '(dynamic) => Category' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform')
What am I doing wrong that is causing this error?

method .map on Map object has to return Map object too.
try this
final categoriesMap = parsedJson['categories'] as Map;
final List<Category> categories =
categoriesMap.entries
.map((e) => Category(key: e.key, category: e.value))
.toList();
entries from a Map returns an Iterable of Map Entry. then you can iterate through it and use key and value properties.

Related

Exception has occurred. _TypeError (type 'FavoriteItem' is not a subtype of type 'Map<String, dynamic>')

I am a beginner in flutter and am trying to save favorite items to a separate screen and this is my controller.
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import '../models/add_to_favorites.dart';
class AddToFavoritesController extends GetxController {
// creating a list of favorites
var favoriteItems = <FavoriteItem>[].obs;
// creating an instance of favorites controller available everywhere
static AddToFavoritesController instance =
Get.put(AddToFavoritesController());
// override onit method
#override
void onInit() {
List? storedFavorites = GetStorage().read<List>('favorites');
if (storedFavorites != null) {
favoriteItems =
storedFavorites.map((e) =>
FavoriteItem.fromJson(e)).toList().obs;
}
// storing the values in getx storage
ever(favoriteItems,
(_) => GetStorage().write('favorites', favoriteItems.toList()));
super.onInit();
}
}
My model is here as follows
class FavoriteItem {
String? name;
String? url;
bool done;
FavoriteItem({ this.name, this.done = false, this.url});
factory FavoriteItem.fromJson(Map<String, dynamic> json) => FavoriteItem(
name: json['name'] as String?,
done: json['done'],
url: json['url'] as String?,
);
FavoriteItem.fromMap(Map map)
: name = map['name'] as String,
url = map['url'] as String,
done = map['done'];
Map<String, dynamic> toJson() => {'name': name, 'done': done, 'url': url};
}
I am able to add items to the next screen and persist the data but when I try to go
to my saved items again from a different screen I get the error "Exception has
occurred. _TypeError (type 'FavoriteItem' is not a subtype of type 'Map<String,
dynamic>')"
I don't know what the problem is. The error points at the "e" in the fromJson method in my controller.

Instance members can't be accessed from a factory constructor. Flutter Firebase

I have two problems in my code , the first one is showing on _patientFromJson
and second one is The argument type 'Object' can't be assigned to the parameter type 'Map<String, dynamic>' that showing on snapshot.data()!
class Patient{
String? name ;
String? pass ;
String? image ;
String? genre ;
DateTime? birth ;
Patient(this.name, this.pass, this.genre,
this.birth,
{this.reference});
DocumentReference? reference;
factory Patient.fromSnapshot(DocumentSnapshot snapshot) {
Patient newPatient = Patient.fromJson(snapshot.data()!);
newPatient.reference = snapshot.reference;
return newPatient;
}
factory Patient.fromJson(Map<String, dynamic> json) =>
_patientFromJson(json);
Map<String, dynamic> toJson() => _patientToJson(this);
#override
String toString() => 'name $name';
Patient _patientFromJson(Map<String, dynamic> data) {
return Patient(
data['name'],
data['pass'],
data['birth'],
data['genre'],
);
}
Map<String, dynamic> _patientToJson(Patient instance) {
return {
'name' : instance.name,
'pass': instance.pass,
'birth': instance.birth,
'genre': instance.genre,
};
}
}
Convert your snapshot data to map data
Patient newPatient = Patient.fromJson(snapshot.data()! as Map<String, dynamic>');
Updated for the first one
Try removing the reference to the instance member. And you may try with
factory Patient.fromJson(Map<String, dynamic> data) =>
Patient(
data['name'],
data['pass'],
data['birth'],
data['genre'],
);
For more about instance_member_access_from_factory read this documentation

Conversion error from object to json and back in flutter

I am trying to convert a list of objects as a json string in shared preferences.
Object class
SuggestionModel suggestionModelFromJson(String str) =>
SuggestionModel.fromJson(json.decode(str));
String suggestionModelToJson(SuggestionModel data) =>
json.encode(data.toJson());
class SuggestionModel {
SuggestionModel({
this.category,
this.icon,
this.subs,
});
eCategory? category;
IconData? icon;
List<Sub>? subs;
factory SuggestionModel.fromJson(Map<String, dynamic> json) =>
SuggestionModel(
category: json["category"],
icon: json["icon"],
subs: List<Sub>.from(json["subs"].map((x) => Sub.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"category": category,
"icon": icon,
"subs": List<dynamic>.from(subs!.map((x) => x.toJson())),
};
}
class Sub {
Sub({
this.subCategory,
this.values,
});
String? subCategory;
List<Value>? values;
factory Sub.fromJson(Map<String, dynamic> json) => Sub(
subCategory: json["sub_category"],
values: List<Value>.from(json["values"].map((x) => Value.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"sub_category": subCategory,
"values": List<dynamic>.from(values!.map((x) => x.toJson())),
};
}
class Value {
Value({
this.subName,
this.selected,
});
String? subName;
bool? selected;
factory Value.fromJson(Map<String, dynamic> json) => Value(
subName: json["sub_name"],
selected: json["selected"],
);
Map<String, dynamic> toJson() => {
"sub_name": subName,
"selected": selected,
};
}
When I try to do
List<SuggestionModel> list;
String encodedData = jsonEncode(list);
it gives me an error
Converting object to an encodable object failed: Instance of 'SuggestionModel'
Im not following where the exact issue comes from. tried debugging and still no luck
How can I rectify this?
Update. I've changed the enum to a String and removed the IconData field. And the above issue had resolved.
Now when I try to get the saved Json string and convert that back to list of objects. I get an error
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<SuggestionModel>'
at this line
var t = await _dataManager.getSelectedList(s);
var addedObj = json.decode(json.decode(t!));
//here...
var list = addedObj.map((e) => SuggestionModel.fromJson(e)).toList();
Try this:
String encodedData = jsonEncode(list.map((e) => e.toJson()).toList());
So the first thing was to update the enum property of Model class to String and remove IconData.
Then for the second issue.
update the decoding function like
List<SuggestionModel> list =
addedObj.map((e) => SuggestionModel.fromJson(e)).toList();

Flutter error when converting a list to a json object removing some keys

I have an error when trying to convert a list of my object to json
My error:
Unhandled Exception: type 'RxList<ItemStockEntryModel>' is not a subtype of type 'Map<dynamic, dynamic>'
My model code:
class StockEntryModel {
final int? id;
final double costFreight;
final List<ItemStockEntryModel> items;
StockEntryModel({
this.id,
required this.costFreight,
required this.items,
});
factory StockEntryModel.fromJson(Map<String, dynamic> json) =>
StockEntryModel(
id: json['id'],
costFreight: json['costFreight'],
items: json['itemStockEntries'],
);
Map<String, dynamic> toJson() => {
'id': id,
'costFreight': costFreight,
'itemStockEntries': items,
};
Map<String, dynamic> itemsToMap() => {
'data': items,
};
String itemsToJson() {
var data = {};
final test = itemsToMap()['data'];
final mappedItems = Map<String, dynamic>.from(test) // the error occurs here on test variable
..removeWhere((key, value) => value == null || key == 'product');
print(json.encode(mappedItems));
data['itemStockEntries'] = mappedItems;
return json.encode(data);
}
}
my goal is to return a json object like this
// is not complete, only example...
{
"itemStockEntries": {
"data": [{
"id": 2
}, {
"id": 3
}]
}
}
but i need remove keys if this value is null and my key product..
I saw some similar errors, but I couldn't find the one that actually causes it
sorry for my bad english =(
My solution based on Loren codes. I expect to help someone also
Map<String, dynamic> toJson() => {
'id': id,
'costFreight': costFreight,
'itemStockEntries': items.map((e) => e.toJson()).toList(),
};
Map<String, dynamic> itemsToMap() => {
'data': items
.map(
(e) => e.toJson()
..removeWhere(
(key, value) => key == 'product' || value == null),
)
.toList(),
};
Map<String, dynamic> modelToJson() {
Map<String, dynamic> data = {};
data['itemStockEntries'] = itemsToMap();
data['costFreight'] = costFreight;
print(json.encode(data));
return data;
}
The .from method on a map needs a map to be passed into it, and you're passing in a list. So removeWhere is looking for keys and values which don't exist the way you're doing it.
So you could clear that first error getting rid of the itemsToMap function and changing the first 2 lines of your itemsToJson function to this.
var data = {'data': items}; // an actual map that you can pass in
final mappedItems = Map<String, dynamic>.from(data) // no more error here
But that's still a map with just a single key with a value of a list. So the removeWhere is not going to do anything of value here.
The List<ItemStockEntryModel> is what you need to be iterating through.
Assuming you have json serialization setup in your ItemStockEntryModel, this is closer to what you need to do. Not a complete example because I don't know what that model looks like, but it should give you the idea.
String itemsToJson() {
Map data = {};
List<String> jsonList = []; // new list of json strings to pass into data map
for (final item in items) {
if (// item meets whatever conditions you need) {
final jsonItem = json.encode(item);
jsonList.add(jsonItem);
}
}
data['itemStockEntries'] = {'data': jsonList};
return json.encode(data);
}

Add comma separated value to class list

I need to add the value in the list which is comma separated.
Sample data:
English,Hindi,French
Below is the class of List:
class LanguageService {
}
class Language extends Taggable {
final String name;
/// Creates Language
Language({
this.name,
// this.position,
});
#override
List<Object> get props => [name];
/// Converts the class to json string.
String toJson() => ''' {
"name": $name,\n
}''';
//}
String thuJson() => ''' {
"name": $name,
}''';
}
GetTags getTagsFromJson(String str) => GetTags.fromJson(json.decode(str));
class GetTags {
List<Content> content;
bool success;
//String error;
GetTags({
this.content,
this.success,
});
factory GetTags.fromJson(Map<String, dynamic> json) => GetTags(
content: (json["content"] as List).map((x) => Content.fromJson(x)).toList(),
success: json["success"],
);
}
class Content {
String tagname;
Content({
this.tagname,
});
factory Content.fromJson(Map<String, dynamic> json) => Content(
tagname: json == null ? 'Empty' : json["tagname"]
);
}
I tried split but it is giving me error.
List<Language> _selectedLanguages;
_selectedLanguages = [];
//responseBody['user_lang'] = 'English,Hindi,French' Data looks like this
_selectedLanguages = responseBody['user_lang'].split(', ');
Exception Caught: type 'List<String>' is not a subtype of type 'List<Language>'
Also tried.
_selectedLanguages.add(responseBody['user_lang']);
Exception Caught: type 'String' is not a subtype of type 'Language'
Update
I tried this too but getting error.
List _dbLanguages = responseBody['user_lang'].split(', ');
selectedLanguages = _dbLanguages.map<List<Language>>((item) => Language(item))
A value of type 'Iterable<List<Language>>' can't be assigned to a variable of type 'List<Language>'.
Try changing the type of the variable, or casting the right-hand type to 'List<Language>'.
One way you can do this is like this.
List<Language> _selectedLanguages;
_selectedLanguages = (responseBody['user_lang'].split(',') as List<String>).map((text) => Language(name: text)).toList();
Dart has a very good type checking system and I think your problem is an obvious one based on the error message. You must convert your list of String into a list of Language. I don't recall the syntax from the top of my head but I think you should be able to convert your list of String with .map<List<Language>>((item) => Language(item))