从Flutter中的地图中获取项目。

huangapple go评论77阅读模式
英文:

taking items from map in flutter

问题

我正在尝试使用映射来从字符串中获取单词并将它们映射到小部件。
我尝试过这样做,但我的问题是单词"doe"和"sister"的键相同,所以最终只能获取其中一个

String theText = "my name is doe from ```http.doe.com```, my sister is selly. doe and saqil are not sister friends of koiter.";

wordsMap = Map.fromIterable(text.split(' '),
         key: (v) => v,
         value: (v) => TextSpan(text: v));

所以我尝试了下面的代码

Map mapMyWord = {};
var splitForSize = text.split(' ').toList();
for(var t = 0; t <= splitForSize.length-1; t++){  
    mapMyWord[t] = {'$t': TextSpan(text: splitForSize[t])};
}

但在第二段代码中,当我尝试访问 mapMyWord.values.toList() 时,它再次返回一个映射数据的列表

[{0: TextSpan("my")}, {1: TextSpan("name")}, {2: TextSpan("is")}, {3: TextSpan("doe")}, {4: TextSpan("```http.codeish.com```",), ... ,{19: TextSpan("koiter")}]

所以我的主要问题是如何从这里获取值。

英文:

I am tring to use a map to get words from a string and map them to a widget.
I have tried this but my problem is the key for the words doe and sister get the same keys so i end up getting only one of them

String theText = "my name is doe from http.doe.com, my sister is selly. doe and saqil are not sister friends of koiter.";

  wordsMap = Map.fromIterable(text.split(&#39; &#39;),
         key: (v) =&gt; v,
         value: (v) =&gt; TextSpan(text: v));

so I tried the code below

  Map mapMyWord = {};
 // var wordsMap;
  var splitForSize = text.split(&#39; &#39;).toList();
  for(var t = 0;t&lt;= splitForSize.length-1;t++){  
    mapMyWord[t] = {&#39;$t&#39;:TextSpan(text: splitForSize[t])};
  }

but In the second code when I tried to access mapMyWord.values.toList() it returns a list of map data again

[{0: TextSpan(&quot;my&quot;)}, {1: TextSpan(&quot;name&quot;)}, {2: TextSpan(&quot;is&quot;)}, {3: TextSpan(&quot;doe&quot;)}, {4: TextSpan(&quot;````http.codeish.com````,&quot;)}, ... ,{19: TextSpan(&quot;koiter&quot;)}]

so my main problem is how to get the values from here.

答案1

得分: 1

它返回地图,因为您正在使用以下行分配地图:

mapMyWord[t] = {'$t':TextSpan(text: splitForSize[t])};

所以最终您将获得一个Map<Int, Map<String, TextSpan>>

如果您想将该句子的单词转换为TextSpan的列表,可以使用以下方式:

var textSpanList = text.split(" ").map((word) => TextSpan(text: word)).toList();

如果您想直接在小部件树中执行此操作,可以使用以下方式:

children: <Widget>[
  for(var word in text.split(" "))
    Text(word),
]

注:最后一段代码需要在pubspec.yaml文件中将最低SDK版本设置为2.2.2

英文:

It returns maps because you're assigning maps with this line :

mapMyWord[t] = {&#39;$t&#39;:TextSpan(text: splitForSize[t])};

So in the end you have a Map&lt;Int, Map&lt;String, TextSpan&gt;&gt;.

If you meant to turn the words of that sentence into a list of TextSpan, this would be the way :

var textSpanList = text.split(&quot; &quot;).map((word) =&gt; TextSpan(text: word)).toList();

If you want to do it directly in the widget tree, this would do it :

children: &lt;Widget&gt;[
            for(var word in text.split(&quot; &quot;))
              Text(word),
]

N.B: This last snippet requires a minimum SDK of 2.2.2 in the pubspec.yaml

答案2

得分: 1

在你的第二段代码中,更改赋值部分:

Map mapMyWord = {};
// var wordsMap;
var splitForSize = text.split(' ').toList();
for (var t = 0; t <= splitForSize.length - 1; t++) {
    mapMyWord[t] = TextSpan(text: splitForSize[t]);
}

然后,mapMyWord.values.toList() 仅会返回一个 TextSpan 列表。如果你想从地图中获取特定的值:

int index = 1; //某个数字
print(mapMyWord[index]); //这将返回一个 TextSpan
英文:

In your second code, change the assignment part:

    Map mapMyWord = {};
    // var wordsMap;
    var splitForSize = text.split(&#39; &#39;).toList();
    for(var t = 0;t&lt;= splitForSize.length-1;t++){  
        mapMyWord[t] = TextSpan(text: splitForSize[t]);
    }

Then, mapMyWord.values.toList() will only return a list of TextSpan's. And if you want to get some specific value from the map:

    int index = 1; //some number
    print(mapMyWord[index]); //this will return one TextSpan

huangapple
  • 本文由 发表于 2020年1月3日 19:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定