Flutter: How to update model item inside list - list

I have a List<Items> items of model Items
class Items {
String name;
String? value;
String price;
}
And i have a Map<String, String> values witch contains values for model. As a key it uses same data as Items.name and value of it contains data.
So my question is. How can i update my items list with added value data from values map? Basically, list of items contains only required field name. Later i get values for it and place it inside map. Now i want to update my list with added value data

This is how you structure the class to accept the Map<String, String> values:
class Item {
String? name;
String? value;
Item({this.name, this.value});
Item.fromJson(Map<String, dynamic> json) {
name = json['name'];
value = json['value'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = name;
data['value'] = value;
return data;
}
}
If this is your map:
Map<String, String> values = {"name": "wambua", "value": "Programmer"};
This is how you map the map to the class:
Item.fromJson(values);

Related

Flutter Dart How insert value on List<Model>

hello all I have an app in flutter and i have List Model I'm new in flutter
hello all I have an app in flutter and i have List Model I'm new in flutter
class OrderePostModel {
OrderePostModel({
this.id,
this.itemsTbId,
this.accessoriesTbId,
this.userTbId,
this.waitingTime,
this.isReady,
this.isWaiting,
this.isClosed,
});
String? id;
String? itemsTbId;
String? accessoriesTbId;
String? userTbId;
String? waitingTime;
bool? isReady;
bool? isWaiting;
bool? isClosed;
factory OrderePostModel.fromJson(Map<String, dynamic> json) =>
OrderePostModel(
id: json["id"],
itemsTbId: json["itemsTBId"],
accessoriesTbId: json["accessoriesTBId"],
userTbId: json["userTBId"],
waitingTime: json["waitingTime"],
isReady: json["isReady"],
isWaiting: json["isWaiting"],
isClosed: json["isClosed"],
);
Map<String, dynamic> toJson() => {
"id": id,
"itemsTBId": itemsTbId,
"accessoriesTBId": accessoriesTbId,
"userTBId": userTbId,
"waitingTime": waitingTime,
"isReady": isReady,
"isWaiting": isWaiting,
"isClosed": isClosed,
};
}
want to add Value in This list ?
like itemsTBId = 9;
You can change the model as below:
List<String>? itemsTBIds

How to convert or equalize Query<Map<String, dynamic>> to List

I have BrandList in my Firebase like this;
How can I convert or equalize this Firebase List to List.
I tried this;
var brandsRef = _firestore.collection("vehicles1").where("Brands");
List brandsList = brandsRef;
But I got this error "A value of type 'Query<Map<String, dynamic>>' can't be assigned to a variable of type 'List'."
You need to use the document Id to get the query and then you can get the data which returns a Map.
From that Map, you can supply the key to retrieve the value. In this case, the key is "Brands".
var brandsQuery = await _firestore.collection("vehicles1").doc(document Id).get();
List brandList = brandsQuery.data()["Brands"];
First I would suggest to create a model of your class Brand in addition to the jsonSerialization classics:
class Brands {
Brands({this.brandName});
List<String> brandName;
Map<String, dynamic> toMap() {
return {
'Brands': brandName,
};
}
factory Brands.fromMap(Map<String, dynamic> map) {
return Brands(
brandName: List<String>.from(map['Brands']),
);
}
String toJson() => json.encode(toMap());
factory Brands.fromJson(String source) => Brands.fromMap(json.decode(source));
}
Then you need to add a few steps to the way you retreive elements:
var response = _firestore.collection("vehicles1").where("Brands").get();
final results =
List<Map<String, dynamic>>.from(response.docs.map((e) => e.data()));
Brands brands =
results.map((e) => Brands.fromMap(e)).toList();

How to add elements dynamically in a 2d list from another list in Flutter?

I have a list of model class objects. Such as -
List<ModelChannel> allChannels = [];
I have added elements in this list from json. Model Class variables are-
final String channelid;
final String channelname;
final String channeltype;
final String categoryname;
final String channelimage;
final String channelurl;
Here categorytype contains country information. I want to divide the list country wise dynamically. I have intended to use 2d list where each row will contain all the channels of a specific country. Is this the right approach? If yes how to implement this and if not what will be the right one?
If I understand correctly, you are looking for groupBy function from collection package.
Add this package to your pubspec.yaml:
dependencies:
collection: any
And use groupBy:
import 'package:collection/collection.dart';
...
final groupByCountry = groupBy(allChannels, (ModelChannel e) => e.categoryname);
List<List<ModelChannel>> countryList = [];
List<String> channelType = [];
allChannels.forEach((element) {
if (channelType.isEmpty) {
channelType.add(element.channeltype);
} else {
if (channelType.contains(element.channeltype)) {
} else {
channelType.add(element.channeltype);
}
}
});
channelType.forEach((countryCode) {
List<ModelChannel> t = [];
allChannels.forEach((element) {
if (element.channeltype == countryCode) {
t.add(element);
}
});
countryList.add(t);
});

Search and find the string in List of DTO class in flutter?

I have this DTO class and in response of API i get the list of this :
class ProjectCode {
String id;
String projectCode;
String projectTitle;
ProjectCode({this.id, this.projectCode, this.projectTitle});
ProjectCode.fromJson(Map<String, dynamic> json) {
id = json['Id'];
projectCode = json['ProjectCode'];
projectTitle = json['ProjectTitle'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Id'] = this.id;
data['ProjectCode'] = this.projectCode;
data['ProjectTitle'] = this.projectTitle;
return data;
}
}
now how can i search in list and find the ProjectTitle that i need to find it and that method return the id in DTO ?
This is simple, you can use this method to find the String that you need to find. make method like this for everything do you need to search like this :
String find(List<ProjectCode> projectCodeList, String projectTitle) {
return projectCodeList
.firstWhere(
(projectCode) => projectCode.projectTitle.contains(projectTitle))
.id;
}

Adding elements to List in Flutter from For statement?

I'm receiving the following error while trying to add elements from my for loop to my List...
NoSuchMethodError: The method 'addAll' was called on null.
Receiver: null
Tried calling: addAll("LrWr826cd3Y")
Here is my code...
Future getData() async {
//Map videoId;
String url = 'https://Youtube API';
var httpClient = createHttpClient();
var response = await httpClient.read(url);
Map data = JSON.decode(response);
var videos = data['items']; //returns a List of Maps
List searchTitles;
List searchIds;
List searchImages;
for (var items in videos) {
//iterate over the list
Map myMap = items; //store each map
final video = (myMap['id'] as Map);
print(video['videoId']);
searchIds.addAll(video['videoId']);
final details = (myMap['snippet'] as Map);
final videoimage = (details['thumbnails'] as Map);
final medium = (videoimage['medium'] as Map);
}
setState(() { });
if (!mounted) return;
}
print(video['videoId']); successfully lists the 3 Youtube video ids as Strings. searchIds.addAll(video['videoId']); throws the error. I've tried both searchIds.add and searchIds.addAll. Where am I going wrong?
I would like to eventually push these lists to my List class here..
class CardInfo {
//Constructor
List id;
List title;
List video;
CardInfo.fromJson(List json) {
this.id;
this.title;
this.video;
}
}
You are not instantiating your searchIds object. add this
List searchIds = new ArrayList<>();
(Or)
List searchIds = new List();