Obtaining Map<String, Element> rather than Map<dynamic, dynamic> - json

We start with a class Element for a member of the periodic table.
import 'dart:convert' as JSON;
class Element {
String name;
Element({this.name = ""});
String toString() {
return this.name.toString();
}
}
We can then decode a JSON string
void f1() {
var jsonStr = """{ "Na": { "name": "Sodium" }, "Ca": { "name": "Calcium" } }""";
final json = JSON.jsonDecode(jsonStr);
print(json.runtimeType); // MappedListIterable<dynamic, dynamic>
final elements = json.map((key, value) => MapEntry(key, Element(name: value['name'])));
print(elements.runtimeType); // _InternalLinkedHashMap<dynamic, dynamic>
}
but suppose we want to specify the type of the result:
Map<String, Element> f3() {
var jsonStr = """{ "Na": { "name": "Sodium" }, "Ca": { "name": "Calcium" } }""";
final json = JSON.jsonDecode(jsonStr);
final elements = Map<String, Element>.fromIterable(json,
key: (k, v) => k,
value: (k, v) => Element.fromJson(name: v['name'])
);
return elements;
}
How can I get around the following error?
Error: The argument type 'String Function(dynamic, dynamic)' can't be assigned
to the parameter type 'String Function(dynamic)?'.
to ensure that the returned object is of type Map<String, Element>?

The signature for Map<K, V>.fromIterable is:
Map<K, V>.fromIterable(
Iterable iterable,
{K key(
dynamic element
)?,
V value(
dynamic element
)?}
)
Note that key: and value: both take functions of only one argument. The idea for key is to take an element from your iterable, and make a Key out of it - in your case, a String. Then for the value callback, you take the same element and make a Value out of it - in your case, an Element.
Also, a map is not iterable, but Map.entries is, so you need to iterate through json.entries, instead of just json.
So something like the following should get the job done:
final elements = Map<String, Element>.fromIterable(json.entries,
key: (entry) => entry.key,
value: (entry) => Element(name: entry.value['name']),
);

Related

Flutter - How to convert Map<String, String> from TextEditingController to a Map<String, dynamic> JSON

I have about 40 TextFormFields and I retrieve their values with TextEditingController. The values are converted into a Map<String, String> map via the following step:
// map that stores controllers
Map<String, TextEditingController> storeControllers = controllers;
// convert to map that stores only controller texts
Map<String, String> currentSelections = storeControllers
.map((key, value) => MapEntry(key, storeControllers[key]!.text))
The current output with all values in String type:
//currentSelections map
Map<String, String>
{
"field1": "1",
"field2": "Two",
"field3": "0.03",
...
"field40": "four40",
}
How do I convert the currentSelections map into a JSON that stores the values in their corresponding types?
//Desired output:
Map<String, dynamic>
{
"field1": 1, //int
"field2": "Two", //String
"field3": 0.03, //double
...
"field40": "four40", //String
}
Any help would be appreciated! :)
I understand that the way to convert Strings to other types is using int.parse("text") method. But how do I do it with so many different types involved?
Maybe try with this
Map<String, dynamic> convert(Map<String, String> map) {
return {
for (final entry in map.entries)
entry.key: int.tryParse(entry.value) ??
double.tryParse(entry.value) ??
entry.value
};
}
Example:
import 'dart:convert';
void main() {
Map<String, String> map = {
"field1": "1",
"field2": "Two",
"field3": "0.03",
"field40": "four40",
};
final newMap = convert(map);
print(jsonEncode(newMap));
//output: {"field1":1,"field2":"Two","field3":0.03,"field40":"four40"}
}
You could use the map method to go through each element and cast it if necessary.
bool isInt(num value) => (value % 1) == 0;
final Map<String, dynamic> desireddMap = currentMap.map((key, value) {
dynamic newValue = value;
// Check if value is a number
final numVal = num.tryParse(value);
if (numVal != null) {
// If number is a int, cast it to int
if (isInt(numVal)) {
newValue = numVal.toInt();
} else {
// Else cast it to double
newValue = numVal.toDouble();
}
}
return MapEntry(key, newValue);
});

How can i convert a JSON String to List<String> in flutter?

I`m trying to retrieve the content of a table from oracle apex to my flutter app with http.get method, and atribute the values to a class i created. Problem is that 3 of the atributes of this class need to be List, so, when i try to map it, it returns this error: [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'String' is not a subtype of type 'List' in type cast.
this is the JSON:
{
"items": [
{
"id": "1",
"nome": "Feijão Tropeiro",
"id_dia_da_semana": "seg",
"id_categoria": "ga",
"url_da_imagem": "https://live.staticflickr.com/65535/52180505297_2c23a61620_q.jpg",
"ingredientes": "vários nadas"
}
],
and this is the class:
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
class Meal {
final String id;
final String descricao;
final List<String> ingredients;
final List<String> idDiaSem;
final List<String> idCategory;
final String imageUrl;
const Meal({
required this.id,
required this.descricao,
required this.ingredients,
required this.idDiaSem,
required this.idCategory,
required this.imageUrl,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'nome': descricao,
'ingredientes': ingredients,
'id_dia_da_semana': idDiaSem,
'id_categoria': idCategory,
'url_da_imagem': imageUrl,
};
}
factory Meal.fromMap(Map<String, dynamic> map) {
return Meal(
id: map['id'] as String,
descricao: map['nome'] as String,
ingredients: map['ingredientes'] as List<String>,
idDiaSem: map['id_dia_da_semana'] as List<String>,
idCategory: map['id_categoria'] as List<String>,
imageUrl: map['url_da_imagem'] as String,
);
}
String toJson() => json.encode(toMap());
factory Meal.fromJson(String source) =>
Meal.fromMap(json.decode(source) as Map<String, dynamic>);
}
can anyone help me please to fix this error? i`ve tried to convert it unsuccessfully
When you have a string you want to parse to a list, there should be a separator. For example, let's suppose this strings:
// The separator here is a comma with a space, like ', '
String str1 = 'ingredient1, ingredient2, bread, idunno, etc';
// The separator here is a simple space, ' '
String str2 = 'ingredient1 ingredient2 bread idunno etc';
Once you identified the separator in the string, you may want to use the string split method in dart, specifying the separator. For example:
// Separator is a simple comma with no spaces, ','
String str1 = 'ingredient1,ingredient2,bread,idunno,etc';
// Splits the string into array by the separator
List<String> strList = str1.split(',');
// strList = ['ingredient1', 'ingredient2', 'bread', 'idunno', 'etc'];
More information on the split dart method at https://api.dart.dev/stable/2.14.4/dart-core/String/split.html
EDIT: Example with your code, supposing the property ingredients is a string that represents an array of strings, separated with ",":
factory Meal.fromMap(Map<String, dynamic> map) {
return Meal(
id: map['id'] as String,
descricao: map['nome'] as String,
ingredients: (map['ingredientes'] as String).split(','),
// ...
You can't cast them to List<String> because they simply aren't a list. If you want them to be a List with a single element you could do this instead:
ingredients: [map['ingredientes'] as String],
idDiaSem: [map['id_dia_da_semana'] as String],
idCategory: [map['id_categoria'] as String],
or make sure the JSON has them as list like
{
"items": [
{
"id": "1",
"nome": "Feijão Tropeiro",
"id_dia_da_semana": ["seg"],
"id_categoria": ["ga"],
"url_da_imagem": "https://live.staticflickr.com/65535/52180505297_2c23a61620_q.jpg",
"ingredientes": ["vários nadas"]
}
],
try this:
static List<Meal> fromMap(Map<String, dynamic> map) {
List<Meal> result = [];
for(var item in map['items']){
result.add(Meal(
id: item['id'] as String,
descricao: item['nome'] as String,
ingredients: item['ingredientes'] as String,
idDiaSem: item['id_dia_da_semana'] as String,
idCategory: item['id_categoria'] as String,
imageUrl: item['url_da_imagem'] as String,
))
}
return result;
}

How to access JSON nested data in flutter dart

I have the below json data, wherein I want to access the values inside the feeling_percentage.
{
"status": "200",
"data": {
"feeling_percentage": {
"Happy": "0",
"Sad": "0",
"Energetic": "0",
"Calm": "0",
"Angry": "0",
"Bored": "0"
},
}
}
I am able to fetch it using the below API code
Future<List<Data>> makePostRequest() async {
List<Data> list = [];
final uri = Uri.parse('<api>');
final headers = {'Content-Type': 'application/json', 'X-Api-Key':'<api_key>'};
Map<String, dynamic> body = {'user_id': 3206161992, 'feeling_date': '15-04-2022'};
String jsonBody = json.encode(body);
final encoding = Encoding.getByName('utf-8');
Response response = await post(
uri,
headers: headers,
body: jsonBody,
encoding: encoding,
);
int statusCode = response.statusCode;
String responseBody = response.body;
print('response body'+ responseBody);
}
After reading few articles, still not able to figure out how do I access the percentage of happy, sad inside the feeling_percentage.
I have created the model as
class Data{
FeelingPercentage feelingPercentage;
Data(
{required this.feelingPercentage});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
feelingPercentage: FeelingPercentage.fromJson(json["data"]),
);
}
}
class FeelingPercentage {
String? happy;
String? sad;
String? energetic;
String? calm;
String? angry;
String? bored;
FeelingPercentage({this.happy, this.sad, this.energetic, this.calm, this.angry, this.bored});
factory FeelingPercentage.fromJson(Map<String, dynamic> json) {
return FeelingPercentage(
happy: json["happy"] as String,
sad: json["sad"] as String,
energetic: json["energetic"] as String,
calm: json["calm"] as String,
angry: json["angry"] as String,
bored: json["bored"] as String,
);
}
}
Another way:
import 'package:fast_json/fast_json_selector.dart' as parser;
void main() {
final path = '{}.data.{}.feeling_percentage';
final level = path.split('.').length;
void select(parser.JsonSelectorEvent event) {
final levels = event.levels;
if (levels.length == level && levels.join('.') == path) {
print(event.lastValue);
event.lastValue = null;
}
}
parser.parse(_source, select: select);
}
const _source = '''
{
"status": "200",
"data": {
"feeling_percentage": {
"Happy": "0",
"Sad": "0",
"Energetic": "0",
"Calm": "0",
"Angry": "0",
"Bored": "0"
}
}
}''';
Output:
{Happy: 0, Sad: 0, Energetic: 0, Calm: 0, Angry: 0, Bored: 0}
You can use this website to convert your JSON object to a dart class.
it automatically creates the fromJson function, which can be used to pass JSON and receive the Dart objects.
Change this line in your model feelingPercentage: FeelingPercentage.fromJson(json["data"]), to feelingPercentage: FeelingPercentage.fromJson(json["data"]["feeling_percentage"]),
This will fix your issue.
You can do a JSON decode that will will result in a map, and then do the assigned like you are doing on your from Json factory, but as another constructor instead:
Class
Todo.fromMap(Map map) :
this.title = map['title'],
this.completed = map['completed'];
In use
Todo.fromMap(json.decode(item))
First decode response.body, then create FeelingPercentage object from json["data"]["feeling_percentage"] map.
Future<FeelingPercentage> makePostRequest() async {
...
final json = json.decode(response.body);
return FeelingPercentage.fromJson(json["data"]["feeling_percentage"])
}
class FeelingPercentage {
String? happy;
String? sad;
String? energetic;
String? calm;
String? angry;
String? bored;
FeelingPercentage({this.happy, this.sad, this.energetic, this.calm, this.angry, this.bored});
factory FeelingPercentage.fromJson(Map<String, dynamic> json) {
return FeelingPercentage(
happy: json["Happy"] as String,
sad: json["Sad"] as String,
energetic: json["Energetic"] as String,
calm: json["Calm"] as String,
angry: json["Angry"] as String,
bored: json["Bored"] as String,
);
}
}

parse json in flutter with map type

i don't understand how parse json to list or any types in flutter
https://jsonplaceholder.typicode.com/photos <= this is json example what i use
and that info is surrounded by [], {}
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
List<Photo> simple =
parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
i expect when json.decode.cast() work, parsed contain each objects but
when i print parsed, it's just one array like [~~~]
why they use cast method with Map<>?
jsonDecode already gives you the list object, so you can optimize the code.
In your case , instead of using
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
List<Photo> simple =
parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
try using
final List<Photo> simple = jsonDecode(responseBody).map((item) => Photo(title: item.title)).toList()
and you avoid having a fromJson function
You do not need to cast the array because they are already a list of objects.
You can use the following to get a list of photo objects:
Future<String> getPhotos() async {
var response = await http.get(
'https://jsonplaceholder.typicode.com/photos');
if (response.statusCode == 200) {
var parsed = json.decode(response.body);
List<Photo> simple = parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
print(simple);
}
}
This is the photo class used.
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
Photo(
{this.albumId, this.id, this.title, this.url, this.thumbnailUrl});
factory Photo.fromJson(Map<dynamic, dynamic> json) {
return Photo(albumId: json['albumId'],
id: json['id'],
title: json['title'],
url: json['url'],
thumbnailUrl: json['thumbnailUrl'],
);
}
}

Getting type 'List<dynamic>' is not a subtype of type 'List<...>' error in JSON

I'm decoding a response body and I'm getting the error:
'List<dynamic>' is not a subtype of type 'List<Example>'
I'm parsing a JSON array of JSON objects, one of the fields is a list of objects as well and I suspect my issue stems from that. I am also using the json_serializable library. Below is my code, I omitted some fields out and changed some variable names but it represents the same code:
import 'package:json_annotation/json_annotation.dart';
part 'example_model.g.dart';
#JsonSerializable()
class Example {
(some fields here)
final List<Random> some_urls;
final List<String> file_urls;
const Example({
(some fields here)
this.some_urls,
this.file_urls,
});
factory Example.fromJson(Map<String, dynamic> json) =>
_$ ExampleFromJson(json);
}
#JsonSerializable()
class Random {
final String field_1;
final int field_2;
final int field_3;
final int field_4;
final bool field_5;
constRandom(
{this.field_1, this.field_2, this.field_3, this.field_4, this.field_5});
factory Random.fromJson(Map<String, dynamic> json) => _$RandomFromJson(json);
}
from the .g dart file that json_serializable made (ommited the encoding part):
Example _$ExampleFromJson(Map<String, dynamic> json) {
return Example(
some_urls: (json['some_urls'] as List)
?.map((e) =>
e == null ? null : Random.fromJson(e as Map<String, dynamic>))
?.toList(),
file_urls: (json['file_urls'] as List)?.map((e) => e as String)?.toList(),
}
Random _$RandomFromJson(Map<String, dynamic> json) {
return Random(
field_1: json['field_1'] as String,
field_2: json['field_2'] as int,
field_3: json['field_3'] as int,
field_4: json['field_4'] as int,
field_5: json['field_5'] as bool);
}
This is my future function:
Future<List<Example>> getData(int ID, String session) {
String userID = ID.toString();
var url = BASE_URL + ":8080/example?userid=${userID}";
return http.get(url, headers: {
"Cookie": "characters=${session}"
}).then((http.Response response) {
if (response.statusCode == 200) {
var parsed = json.decode(response.body);
List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
return list;
}
}).catchError((e)=>print(e));
}
This code creates a List<dynamic>
parsed.map((i) => Example.fromJson(i)).toList();
You must explicitly cast List<dynamic> to List<Example> like so,
List<Example> list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
or just
var /* or final */ list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
See also
In Dart, what's the difference between List.from and .of, and between Map.from and .of?
https://api.dartlang.org/stable/2.0.0/dart-core/List/List.from.html
https://api.dartlang.org/stable/2.0.0/dart-core/List/List.of.html
Dart 2.X List.cast() does not compose
Reason for Error:
You get this error when your source List is of type dynamic or Object (let's say) and you directly assign it to a specific type without casting.
List<dynamic> source = [1];
List<int> ints = source; // error
Solution:
You need to cast your List<dynamic> to List<int> (desired type), there are many ways of doing it. I am listing a few here:
List<int> ints = List<int>.from(source);
List<int> ints = List.castFrom<dynamic, int>(source);
List<int> ints = source.cast<int>();
List<int> ints = source.map((e) => e as int).toList();
I was receiving the 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<Example> when i tried Günter's solution.
var parsed = json.decode(response.body);
var list = parsed.map((i) => Example.fromJson(i)).toList();
Casting the parsed data into a List<dynamic> (rather than just letting it go to dynamic) resolved that issue for me.
var parsed = json.decode(response.body) as List<dynamic>;
var list = parsed.map((i) => Example.fromJson(i)).toList();
Your code:
var parsed = json.decode(response.body);
List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
Can be replaced with this code:
import 'package:json_helpers/json_helpers.dart';
final examples = response.body.jsonList((e) => Example.fromJson(e));
And everything will work as you expected...