英文:
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(' '),
key: (v) => v,
value: (v) => TextSpan(text: v));
so I tried the code below
Map mapMyWord = {};
// var wordsMap;
var splitForSize = text.split(' ').toList();
for(var t = 0;t<= splitForSize.length-1;t++){
mapMyWord[t] = {'$t':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("my")}, {1: TextSpan("name")}, {2: TextSpan("is")}, {3: TextSpan("doe")}, {4: TextSpan("````http.codeish.com````,")}, ... ,{19: TextSpan("koiter")}]
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] = {'$t':TextSpan(text: splitForSize[t])};
So in the end you have a Map<Int, Map<String, TextSpan>>
.
If you meant to turn the words of that sentence into a list of TextSpan, this would be the way :
var textSpanList = text.split(" ").map((word) => TextSpan(text: word)).toList();
If you want to do it directly in the widget tree, this would do it :
children: <Widget>[
for(var word in text.split(" "))
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(' ').toList();
for(var t = 0;t<= 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论