1
votes

Looking at the docs for provider it says:

The easiest way to read a value is by using the static method Provider.of(BuildContext context). This method will look up in widget tree starting from the widget associated to the BuildContext passed and it will return the nearest variable of type T found (or throw if nothing if found).

So, basically, you access a variable / value by its type, and not its name. Does this mean that each provider can only have a single variable of each type? What if I have two variables of type String? Do I need to wrap those in a custom type and then access them via that type and then the variable name?

1
I think you are right, you will need to have a custom type to be able to pass two String (for example),Hosar

1 Answers

0
votes

I'm struggling with this too. So far I've sort of gotten around it by creating a map and returning it in the getter. Also a list would work. I hope to find a better way but this is what I've got so far.

class Thing with ChangeNotifier {

  final _things = FullThing().things;

  Map get allThings {
    _things.shuffle();

    Map<String, dynamic> allThings = {
      "h0": _things.sublist(0, 7),
      "h1": _things.sublist(8, 15),
      "h2": _things.sublist(16, 23),
      "h3": _things.sublist(24, 31),
      "h4": _things.sublist(32, 41),
    };

    return allThings;
  }
}