Flutter Dart How insert value on List<Model> - list

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

Related

how to filter list object by using stream map filter in java

i want to filter a list of student in java. I have a student class in kotlin like this.
class Student(
var id: String? = null,
var firstName: String? = null,
var lastName: String? = null
) {
constructor(entity: StudentCourse?): this() {
if (entity != null) {
this.id = entity.id.id
this.name = entity.name
}
}
}
class StudentCourse (#EmbeddedId open var id: StudentCourseId) {
constructor() : this(StudentCourseId())
open var name: Boolean? = null
}
#Embeddable
open class StudentCourseId: Serializable {
open var id: String? = null
open var deptName: String? = null
}
this is the list i want to filter :
var students: List<Student> = listOf(
Student("14adbv45", "dan", "GEG"),
Student("96adbv42","Bob", "Bowyer"),
Student("30adbv45","Emily", "Eden")
)
I do this
List<students> studentListContainsFirstNameBob = students.stream()
.map(StudentCourse)
.filter(e -> e.getFirstName.equals("Bob"))
.flatMap(List::stream);
but it doesn't work.
How can i do it please
There are multiple issues in your code.
For example in this part:
constructor(entity: StudentCourse?): this() {
if (entity != null) {
this.id = entity.id.id
this.name = entity.name
}
}
The entity.name refers to StudentCourse#name, but this property is actually of Boolean type, so comparing it to String doe snot make much sense. You have also doubled .id which is incorrect.
Next thing, I am not sure what this snipped should do, was the intention to link a student with given course? If so, you would probably like to add a list of students to a course, or a list of courses to a student (which sounds more correct).
Finally, when it comes to filtering a list, you can get students with first name Bob in this way:
var studentListContainsFirstNameBob: List<Student> = students
.filter { it.firstName.equals("Bob") }
.toList()
Mind the it variable refers to the element from the list, it could also be written:
var studentListContainsFirstNameBob: List<Student> = students
.filter { student -> student.firstName.equals("Bob") }
.toList()

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 convert list getting from future method to Map<String, dynamic>

I want to convert the list coming from getPosts method (getting result from the web json and stored in the posts list) to List
Post Class
class Post {
final int userId;
final String id;
final String title;
final String body;
Post(this.userId, this.id, this.title, this.body);
}
Future<List<Post>> getPosts() async {
var data = await http
.get("https://jsonplaceholder.typicode.com/posts");
var jasonData = json.decode(data.body);
List<Post> posts = [];
for (var i in jasonData) {
Post post = Post(i["userId"], i["id"], i["title"], i["body"]);
posts.add(post);
}
return posts;
}
I tried to put the result directly to this method
static List<Map> convertToMap({List myList }) {
List<Map> steps = [];
myList.forEach((var value) {
Map step = value.toMap();
steps.add(step);
});
return steps;
}
but it's not working, I see this error
The argument type 'List<Map<dynamic, dynamic>>' can't be assigned to the parameter type 'Map<String, dynamic>'.
Change List<Map> by List<Map<String, dynamic>>
static List<Map<String, dynamic>> convertToMap({List myList }) {
List<Map<String, dynamic>> steps = [];
myList.forEach((var value) {
Map step = value.toMap();
steps.add(step);
});
return steps;
}

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;
}

Dart compare unordered Lists with Objects

I have following problem. There are two Lists with Articles in it. In the following example the result prints me false. But the Articles are the same. I think thats, because these are different objects, if I add in both lists article1, it will be true.
I also tried DeepCollectionEquality.unordered().equals from this issue:
How can I compare Lists for equality in Dart?
But it is also giving me FALSE back.
In my real project, I have two Lists with Articles in it. These lists are not sorted, so one Article can be the first in one list, and an article with the same id and name can be the last one in the other list. But if both lists have the same articles (with the same name and id) it should result with true.
var a = List<Article>();
var b = List<Article>();
var article1 = Article(id: "1", name: "Beer");
var article2 = Article(id: "1", name: "Beer");
a.add(article1);
b.add(article2);
print(listEquals(a, b));
As you said , those are different objects, you need to override the equal operator of your class, like this:
class Article {
final String name;
final String id;
const Article({this.id,this.name});
#override
bool operator ==(Object other) =>
identical(this, other) ||
other is Article &&
runtimeType == other.runtimeType &&
id == other.id;
#override
int get hashCode => id.hashCode;
}
Then the result will be true, because the ids are the same.
Also you can take a look at this package
https://pub.dev/packages/equatable
As my solution I used the "equatable"- package: https://pub.dev/packages/equatable
For checking an unordered List, I use DeepCollectionEquality.unordered().equals instead of listEquals
var a = List<Article>();
var b = List<Article>();
var article1 = Article(id: "1", name: "Beer");
var article2 = Article(id: "2", name: "Tequilla");
var article3 = Article(id: "3", name: "VodkaBull");
var article4 = Article(id: "1", name: "Beer");
var article5 = Article(id: "2", name: "Tequilla");
var article6 = Article(id: "3", name: "VodkaBull");
a.add(article1);
a.add(article2);
a.add(article3);
b.add(article4);
b.add(article5);
b.add(article6);
print(listEquals(a, b));
print(DeepCollectionEquality.unordered().equals(a, b));
The Code for my Article looks as following:
import 'package:equatable/equatable.dart';
class Article extends Equatable {
String id;
String name;
Article({this.id, this.name});
#override
// TODO: implement props
List<Object> get props => [id, name];
}