dartdart-shelf

Separating a Cascade in Dart


I'm seeing some weird behavior in Dart. My goal is to only serve static assets if a build/web Dir exists.

1- The following works:

Cascade cc;
if(new Directory(buildPath).existsSync() )
{
  cc = new Cascade().add(apiHandler).add(fHandler);
} else {
  cc = new Cascade().add(apiHandler);
}

2- The following does not work:

  Cascade cc = new Cascade().add(apiHandler);
  if( new Directory(buildPath).existsSync() )
  {
    cc.add(fHandler);
  }

Question: The example in scenario 1 works fine. In the second example, when i add fHandler, how come none of its associated routes ever get handled?


Solution

  • The Cascade class is immutable so the add method returns a new instance. Your second code block is assuming the current instance is modified

    You need to add the cc =

    cc = cc.add(..)