Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 give static values.
Is it possible to load the highlighted words from API like a list instead of map

List=["Flutter", "Words"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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,
  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading