Home > Blockchain >  Dynamically load list of words from API as map in highlight_text: ^1.6.0
Dynamically load list of words from API as map in highlight_text: ^1.6.0

Time:11-29

I need to highlight some words in a text. I used highlight_text 1.6.0.

Map<String, HighlightedWord> words = {
    "Flutter": HighlightedWord(
        textStyle: textStyle,
    ),
    "words": HighlightedWord(
       
        textStyle: textStyle,
    ),
   
};



TextHighlight(
          text: text,
          words: words,
          matchCase: false,
          textStyle: TextStyle(
            fontSize: 14.0,
            color: Colors.black,
          ),
          textAlign: TextAlign.justify,
        )

It is working fine when given static values. Is it possible to load the highlighted words from API like a list instead of the map?

List=["Flutter", "Words"]

CodePudding user response:

Lets assume you get this sample list from api:

List sample = ["Flutter", "Words"];

then you can use fromIterable:

var result = Map<String, HighlightedWord>.fromIterable(sample,
    key: (v) => v,
    value: (v) => HighlightedWord(
          textStyle: textStyle,
        ),
);

then use result in TextHighlight:

TextHighlight(
      text: text,
      words: result,
      matchCase: false,
      textStyle: TextStyle(
        fontSize: 14.0,
        color: Colors.black,
      ),
      textAlign: TextAlign.justify,
  )
  • Related