A syntax error is not necessarily a typo
Many of the most useful questions have revolved around "simple" syntax errors that follow a common pattern. Beginners in the language make the same mistake all the time, and they sincerely expect the code to be meaningful and have a specific meaning that it just doesn't because of the rules of that language.
For example, Python programmers might not realize that a block can't be empty; or be surprised that their mis-indented else
doesn't register as an IndentationError
but instead gives the wrong behaviour, because it aligned with a loop instead (a feature seen in few if any other languages); or (prior to 3.10) get a misleading IndentationError
because a try:
was missing the corresponding except:
; or simply expect a tab to equal a specific number of spaces (which changed in 3.x); or not realize that docstrings have to be indented like normal code (because they're not actually comments). Even if they don't know the absolute basics, it's worth having an explanation.
Similarly for the problem described here: it's very easy to imagine a programmer sincerely expecting the code to work, and having written it entirely deliberately. The intended purpose is clear. Something is learned by pointing out that a different approach is necessary; and simply having someone point to the erroneous line doesn't empower OP to fix the problem. That is: the question isn't really a "debugging" question; it's a how-to question in disguise.
For that matter, code that appears directly within a class
body (not within a method) would work in Python. New Dart programmers might very reasonably have the expectation that OP's code should cause the filters
to include _myFunction
for each instance. Showing how to make the constructor do this instead is useful. (In fact, Python programmers applying Python's rules would expect filters
to be shared between instances of the class, and to be initialized to contain _myFunction
before any instances are created.)
The question should be edited
While the code might all be necessary to demonstrate what OP is trying to do, it isn't all relevant to the problem reported. The code is not expected or required to reflect the real-world problem OP is trying to solve - because OP is not required to be trying to solve a real-world problem, but only to ask a practical question.
A Minimal Reproducible Example needs to be minimal - so the code should only show the relevant class, not a complete program. (It also shouldn't be necessary to define a separate LogEntry
class to demonstrate the problem; why not just use a primitive type?)
However, I have refrained from editing because I basically don't know anything about this language.
The question may be a duplicate
The problem described sounds like something that should be very common. Surely new Dart/Flutter programmers try to write top-level code in a class all the time (since, again, there are other languages where this is well defined and meaningful). A search like [flutter] the name of a constructor
gives me over 100 results - none of which really stands out as a well-asked question, and many of which are surely about other problems; but that's a starting point.
There should be a canonical duplicate for that problem. If one cannot be identified by the Dart/Flutter community, this could become one.