Why is this initialization of trailing fields wrong in Dart?

I have a simple class with two final fields, one of which is a map for storing data and the other is a step function that updates the data when called:

class Data
{

  //for each name a list of observations
  final Map<String,List<double>> _dataMap;

  /**
   * the update step
   */
  final Step _updateStep;

      

Step

is just a typedef

.

Now I need a constructor that has one parameter: a function that takes a reference Map<...>

and returns a new one Step

. It seems logical to me, the updater needs a map link to update it.

Why then does this constructor fail?

  Data(Step initializer(Map<String,List<double>> dataReferences))
  : _dataMap = new Map(),
  _updateStep = initializer(_dataMap);

      

Error in the second step

illegal implicit access to the receiver 'this';

What? How is it leaked? How to fix it?

+3


source to share


2 answers


Gunther Zochbauer has already explained the reason for your mistake.

Here's a workaround:



Data(Step initializer(Map<String,List<double>> dataReferences))
  : this._internal(initializer, new Map());

Data._internal(initializer, map)
  : _dataMap = map,
    _updateStep = initializer(map);

      

+6


source


You are reading from _dataMap

( initializer(_dataMap)

). _dataMap

is a field this

, it is not an argument. You cannot read fields from 'this' in constructor initializers, as the error message says.



+2


source







All Articles