Search code examples
arraysflutterdynamicparametersnamed

how to pass a dynamic optional named paremeter to an array in flutter?


I'm trying to make a dynamic list, is it possible to pass a dynamic optional named parameter in it like this?

    List<dynamic> rtnListIndex = [{rtnListVal}];

  dynamic rtnListVal(int index, {
    String name,
    int tenthMin,
    int min,
    int tenthSec,
    int sec,
    int reps,
    double interval,
    int sets,
    int rest,
    int totalMin,
    int totalSec,
    int type,
    int prep,
  }) {
    return [index = 0, {name, tenthMin, min, tenthSec, sec, reps, interval, sets, rest, totalMin, totalSec, type, prep}];
  }

enter image description here


Solution

  • To answer your question, Lists in dart cannot be passed optional parameters as the only parameters for the list method are specifying a length and is already optional. More about lists in dart here.

    If you just want a List with values in it you can create it like so:

    var rtnListIndex = [0, myValue, 'a string']

    which would make rtnListIndex a List<Dynamic> with as many values in it as you add to the list literal in the assignment statement.

    From looking at your code though it seems like creating a Class or Map would be a better data structure for your apparent use case. You can learn more about these at the provided links.