如何比较我的两个 projectModels 对象,一个来自本地值,另一个来自未来?扑
How to compare my two projectModels objects one is from a local value and the other from a future? flutter
我正在尝试比较两个对象并查看它们是否具有相同的值,其中一个对象来自未来的 api 调用
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
另一个来自本地值,我用另一个 api 调用我调用的 onInit()
final _projectModel = ProjectModel(
guid: '',
projectColor: '',
title: '',
tags: [],
tasks: [],
teamMembers: [],
).obs;
fetchSingleProject() async {
try {
if (_projectModel.value.tasks!.isEmpty) {
_isLoading(true);
}
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
print('from local: ${_projectModel.value.tasks![0].description}');
print('from api: ${response.tasks![0].description}');
// var shouldReload = projectModel.tasks!
// .any((element) => _projectModel.value.tasks!.contains(element));
// bool shouldReload = response.tasks == _projectModel.value.tasks;
const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
bool shouldReload = dc.equals(
response.tags!.toList(),
_tagsList.toList(),
);
print('should the stream reload: $shouldReload');
if (shouldReload) {
_projectModel.value = response;
_tagsList.assignAll(response.tags!);
}
} catch (e) {
throw Get.snackbar('Opss', 'An error occourred');
} finally {
_isLoading(false);
}
}
fetchInitialProject() async {
try {
_isLoading(true);
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
_projectModel.value = response;
_tagsList.assignAll(response.tags!);
} catch (e) {
throw Get.snackbar('Error', '$e');
} finally {
_isLoading(false);
}
}
@override
Future<void> onInit() async {
if (_projectModel.value.tasks!.isEmpty) await fetchInitialProject();
super.onInit();
}
@override
void onReady() {
timer = Timer.periodic(const Duration(seconds: 3), (timer) async {
await fetchSingleProject();
});
super.onReady();
}
当我打印每个对象的值时,它们的结果是一样的,但结果是错误的,为什么会这样,我该如何解决?
print('from local: ${_projectModel.value.tasks![0].description}');
print('from api: ${response.tasks![0].description}');
---
flutter: from local: This is an autogenerated #tbd, @ShemsedinRaketa
flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa
---
尝试比较 tasksList 的方法 1:
bool shouldReload = response.tasks == _projectModel.value.tasks;
// returns false
尝试比较 tasksList 的方法 2:
// importing form the collection package
const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
bool shouldReload = dc.equals(
response.tags!.toList(),
_tagsList.toList(),
);
// returns false as well
尝试比较 tasksList 的方法 3:
var shouldReload = projectModel.tasks!
.any((element) => _projectModel.value.tasks!.contains(element));
// even if all the values are not the same it still returns true when
// only one of the values matches
// flutter: from local: This is an autogenerated #tbd,
// @ShemsedinRaketa
// flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa poakdhmnbs
// flutter: should the stream reload: true
我想用它实现的是一个流,我希望能够控制何时加载来自 api 的新数据,何时加载来自 api 的数据api 与本地相同则不更新 UI.
bool areEqual(List<Person> a, List<Person> b){
for(int i = 0;i<a.length;i++){
if(a[i].id != b[i].id){
return false;
}
}
return true;
}
void main(){
List<Person> a = [Person(),Person()];
List<Person> b = [Person(),Person()];
}
这就是您的函数的样子,但请将其更改为您的对象
不是人,值不是 id,而是对你的描述
我正在尝试比较两个对象并查看它们是否具有相同的值,其中一个对象来自未来的 api 调用
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
另一个来自本地值,我用另一个 api 调用我调用的 onInit()
final _projectModel = ProjectModel(
guid: '',
projectColor: '',
title: '',
tags: [],
tasks: [],
teamMembers: [],
).obs;
fetchSingleProject() async {
try {
if (_projectModel.value.tasks!.isEmpty) {
_isLoading(true);
}
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
print('from local: ${_projectModel.value.tasks![0].description}');
print('from api: ${response.tasks![0].description}');
// var shouldReload = projectModel.tasks!
// .any((element) => _projectModel.value.tasks!.contains(element));
// bool shouldReload = response.tasks == _projectModel.value.tasks;
const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
bool shouldReload = dc.equals(
response.tags!.toList(),
_tagsList.toList(),
);
print('should the stream reload: $shouldReload');
if (shouldReload) {
_projectModel.value = response;
_tagsList.assignAll(response.tags!);
}
} catch (e) {
throw Get.snackbar('Opss', 'An error occourred');
} finally {
_isLoading(false);
}
}
fetchInitialProject() async {
try {
_isLoading(true);
ProjectModel response = await ProjectServices.fetchSingleProjectStream(
projectId: projectId,
);
_projectModel.value = response;
_tagsList.assignAll(response.tags!);
} catch (e) {
throw Get.snackbar('Error', '$e');
} finally {
_isLoading(false);
}
}
@override
Future<void> onInit() async {
if (_projectModel.value.tasks!.isEmpty) await fetchInitialProject();
super.onInit();
}
@override
void onReady() {
timer = Timer.periodic(const Duration(seconds: 3), (timer) async {
await fetchSingleProject();
});
super.onReady();
}
当我打印每个对象的值时,它们的结果是一样的,但结果是错误的,为什么会这样,我该如何解决?
print('from local: ${_projectModel.value.tasks![0].description}');
print('from api: ${response.tasks![0].description}');
---
flutter: from local: This is an autogenerated #tbd, @ShemsedinRaketa
flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa
---
尝试比较 tasksList 的方法 1:
bool shouldReload = response.tasks == _projectModel.value.tasks;
// returns false
尝试比较 tasksList 的方法 2:
// importing form the collection package
const DeepCollectionEquality dc = DeepCollectionEquality.unordered();
bool shouldReload = dc.equals(
response.tags!.toList(),
_tagsList.toList(),
);
// returns false as well
尝试比较 tasksList 的方法 3:
var shouldReload = projectModel.tasks!
.any((element) => _projectModel.value.tasks!.contains(element));
// even if all the values are not the same it still returns true when
// only one of the values matches
// flutter: from local: This is an autogenerated #tbd,
// @ShemsedinRaketa
// flutter: from api: This is an autogenerated #tbd, @ShemsedinRaketa poakdhmnbs
// flutter: should the stream reload: true
我想用它实现的是一个流,我希望能够控制何时加载来自 api 的新数据,何时加载来自 api 的数据api 与本地相同则不更新 UI.
bool areEqual(List<Person> a, List<Person> b){
for(int i = 0;i<a.length;i++){
if(a[i].id != b[i].id){
return false;
}
}
return true;
}
void main(){
List<Person> a = [Person(),Person()];
List<Person> b = [Person(),Person()];
}
这就是您的函数的样子,但请将其更改为您的对象 不是人,值不是 id,而是对你的描述