the modifier '{0}' can't be applied to the body of a setter

######## Solution 1:
If you need to do something asynchronous inside the setter anyway, perhaps log 
something after doing the actual setting, you have a few options.
The simplest is to just call an async helper function:

set foo(Foo foo) { 
  _foo = foo;
  _logSettingFoo(foo);
}

static void _logSettingFoo(Foo foo) async {
  try {
    var logger = await _getLogger();
    await logger.log("set foo", foo);
    logger.release();  // or whatever.
  } catch (e) {
    // report e somehow.
  }
}

######## Solution 2:
This makes it very clear that you are calling an async function where nobody's
waiting for it to complete.
If you don't want to have a separate helper function, you can inline it:

set foo(Foo foo) { 
  _foo = foo;
  void _logSettingFoo() async {
    ...
  }
  _logSettingFoo();
}

########## OR 

set foo(Foo foo) { 
  _foo = foo;
  () async {
    ...foo...
  }();
}

############ Reason For above Solution:
The reason a setter cannot be async is that an async function returns a future,
and a setter does not return anything. That makes it highly dangerous to make a
setter async because any error in the setter will become an uncaught
asynchronous error (which may crash your program). Also, being async probably
means that the operation will take some time, but there is no way for the 
caller to wait for the operation to complete. That introduces a risk of race 
conditions. So, it's for your own protections.

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source