How to create dynamic variables and assign list of data to it using flutter

How to create dynamic variable and how to add list of key,value pair values to it ?(Please read added comments)

Map sample = Map(); // creating sample named dynamic variable

List<TechData> data = [
{"title": 'Android', "date": '10/01/2019'},
{"title": 'Flutter', "date": '10/01/2019'},
{"title": 'Java', "date": '30/10/2019'},
];

sample['Android'] = [{}];  // initializing the dynamic variable

for (var i = 0; i < data.length; i++) {  // trying to add value using .add()
    if (data[i].title == 'Android') {
        sample['Android'].add(data[i]);
    }
}

when adding a value using .add() it causing an error as below.

Exception has occurred.

TypeError (type ‘TechData’ is not a subtype of type ‘Map’ of ‘value’)

Can anyone please provide any solution to solve this error?

1 Like

I think the error is raised due to not initializing the data list variable with TechData objects.

I have tried following code in Dartpad

  class TechData {
    String title;
    String date;
   
   TechData(title, date){
     this.title = title;
     this.date = date;
   }
  }
  
void main() {
  
  Map sample = Map();
  
  List<TechData> data = [
    new TechData('Android', '10/01/2019'),
    new TechData('Android', '10/01/2019'),
    new TechData('Android', '30/10/2019')
    ];
  
  sample['Android'] = [];
  
  for (var i = 0; i < data.length; i++) {
    if (data[i].title == 'Android') {
        sample['Android'].add(data[i]);
    }
  }
  print(sample);
}
1 Like

@sagar Thank you for the reply, But can you please let me know how can I initialize data list variable if it is getting data from API since I have used sample static data while asking a question.

1 Like

Checkout this link : JSON Serialization

Example:

import 'dart:convert';

class TechData {
    String title;
    String date;
   
   TechData(this.title, this.date);
   
   TechData.fromJson(Map<String, dynamic> json)
     : title = json['title'],
       date = json['date'];
  }
  
void main() {
 String jsonStringFromApi = '{"data":[{"title":"Android","data":"10/12/19"},{"title":"Java","data":"10/12/19"},{"title":"Flutter","data":"10/12/19"}]}';
   
 // Here we decode the json as Map
 Map jsonAsMap =  jsonDecode(jsonStringFromApi);
 
 List<TechData> data = [];
 
 jsonAsMap['data'].forEach((json) => data.add(TechData.fromJson(json)));
  
 print(data);
}

Let us know if this resolves your query

3 Likes

@sagar Thanks a lot for providing this solution.

1 Like

Dear Sagar can i print “tittle” and “10/12/19” from jsonstring?
Thx

1 Like

@daniele777
No you cannot access the json elements from jsonStringFromApi variable as it is a plain string.

You can use the decoded json to access the values.

2 Likes

Please can write an example? i can’t understand how to retrive data using loop…

@daniele777

import 'dart:convert';

void main() {
  String jsonStringFromApi =
      '{"data":[{"title":"Android","date":"10/12/19"},{"title":"Java","date":"10/12/19"},{"title":"Flutter","date":"10/12/19"}]}';

  // Here we decode the json as Map
  Map jsonAsMap = jsonDecode(jsonStringFromApi);
  jsonAsMap["data"].forEach((json) => print(json["title"]));
}
2 Likes

ohh thats cool !!! i try!!!

And for print … also “date”…i try…
jsonAsMap[“data”].forEach((json) => print(json[“title”]));
print(json[“date”]));

but don’t work,…mistake dart expression?

2 Likes

i try to display also “date” value but recive error …

jsonAsMap[“data”].forEach((json) => print(json[“title”])),print(json[“date”]));

error…

@daniele777
When you want to write multiline code statement in forEach, we need to use code block similar to following code.

import 'dart:convert';

void main() {
  String jsonStringFromApi =
      '{"data":[{"title":"Android","date":"10/12/19"},{"title":"Java","date":"10/12/19"},{"title":"Flutter","date":"10/12/19"}]}';

  // Here we decode the json as Map
  Map jsonAsMap = jsonDecode(jsonStringFromApi);
  jsonAsMap["data"].forEach((json) {
    print(json["title"]);
    print(json["date"]);
  });
}
4 Likes

Thank you a lot solve problem !!!

2 Likes