简体   繁体   English

如何覆盖 Dart/flutter 上的变量

[英]How to override variables on Dart/flutter

I'm coming from TS/JS background and I was overriding properties using it previous value with something like this in Javasctipt/Typesctipt :我来自 TS/JS 背景,我在Javasctipt/Typesctipt 中使用它以前的值覆盖属性:

let arr = ['messi', 'mo salah'];
arr = arr.filter( name => name != 'messi');

// results
// arr = ['mo salah'];

Now in Dart , I can't do that and I have searched through the docs/internet and didn't find useful answers for the right syntax!现在在Dart 中,我不能这样做,我已经搜索了文档/互联网,但没有找到正确语法的有用答案!

What I did is same as above code:我所做的与上面的代码相同:

var arr = ['messi', 'mo salah'];
arr = arr.where( (name) => name != 'messi' );

// results
nothing showing up

I have seen on some articles for creating a search with the dart that they clearing the arr first then adding the new filter arr:我在一些使用 dart 创建搜索的文章中看到,他们首先清除arr然后添加新的过滤器 arr:

var arr = ['messi', 'mo salah'];
var filter = arr.where( (name) => name != 'messi' );

// then
arr.clear();
arr.addAll(filter);

You can use retainWhere , result: [no salah]您可以使用retainWhere ,结果: [no salah]

List<String> x = ["messi", "no salah"];
x.retainWhere((f) => f != "messi");
print(x);

Your code will create a new list, but this code will update your list.您的代码将创建一个新列表,但此代码将更新您的列表。 You can choose which is useful for your purpose.您可以选择哪个对您的目的有用。

The problem is where method on lists returns a variable with type WhereIterable<String> .问题是列表上的方法返回一个类型为WhereIterable<String>的变量。 You can see it by running the following code:您可以通过运行以下代码来查看它:

 void main() {
   dynamic arr = ['messi', 'mo salah'];
   arr = arr.where((name) => name != 'messi');
   print(arr.runtimeType); // WhereIterable<String>
 }

When you wrote var arr List became the type and as Dart is a strictly-typed language, you can't assign a value of type x to a variable of type y , unless one of them is dynamic.当您编写var arr List 成为类型并且由于 Dart 是一种严格类型的语言,您不能将x类型的值分配给y类型的变量,除非其中一个是动态的。 The dynamic type means that it can take any type, like any in TS. dynamic类型意味着它可以采用任何类型,就像 TS 中的 any 一样。 So you have to call toList() on arr to convert it from iterable to a list:所以你必须在 arr 上调用toList()将它从可迭代转换为列表:

 void main() {
   var arr = ['messi', 'mo salah'];
   arr = arr.where((name) => name != 'messi').toList();
   print(arr); // [mo salah]
 }

I have found the answer to my question while I'm writing it here,我在这里写的时候找到了我的问题的答案,

what I was missing?我错过了什么? is or the difference between filter in TS/JS and where in dart that filter return array type but where is not and I have to convert it to an array by adding toList() at the end.是或之间的差filterTS / JSwhere该过滤器返回array类型,但where不,我必须通过添加将其转换为一个数组toList()结尾。

So my final code in dart should look like this:所以我在 dart 中的最终代码应该是这样的:

var arr = ['messi', 'mo salah'];
arr = arr.where( (name) => name != 'messi' ).toList();

// results
// arr = ['mo salah'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM