0

How can I get a single entry from a map to give it to the widget in Flutter?

For an example:

Map<String, String> text = {
    "text1" : "Text 1",
    "text2" : "Text 2"
}
home: Center(
    child: Text(text["text1"])
)

This shows error. I can't access the key of the map.

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • Does this answer your question? [Flutter/Dart: How to access a single entry in a map/object](https://stackoverflow.com/questions/53824755/flutter-dart-how-to-access-a-single-entry-in-a-map-object) – OMi Shah Aug 26 '23 at 10:45
  • also, post the exact error message you're seeing. – OMi Shah Aug 26 '23 at 10:48
  • All the accesses used in the page you provide link to, are in the print statement. That will print the text in the debug console but I want to pass that text in a widget which will be shown when I run the app, on the screen of the device (Android or iOS). – Lovish Singla Aug 26 '23 at 11:51
  • Only you had to add not null check [doc](https://dart.dev/null-safety/understanding-null-safety)! Only if you had add the error/issue message. :) – OMi Shah Aug 26 '23 at 12:29

1 Answers1

0

Ensure you imported import 'package:flutter/material.dart'

Map<String, String> text = {
    "text1" : "Text 1",
    "text2" : "Text 2"
}

Add a null checker

home: Center(
child: Text(text["text1"]!))
CharlyKeleb
  • 587
  • 4
  • 17