skip to Main Content

I have a unique situation where flutter/dart seems to be requiring a coma for line endings instead of semi colon and I am completely baffled. Is this correct behavior or is there some major bug going on?

SharedPreferences.getInstance().then((prefs) => {
    prefs.setString("auth", sr.data);
    prefs.setString("phone", myPhone);
}

produces the error "Expected to find ‘}’

if I change them to commas its totall fine

SharedPreferences.getInstance().then((prefs) => {
    prefs.setString("auth", sr.data),
    prefs.setString("phone", myPhone),
}

I have a lot more code in here with if statements and other things going on but its all the same. Any semicolon produces the error and commas seem to make everything happy. If this is correct behavior how do I know when commas are needed??

2

Answers


  1. () {} requires semi column

    SharedPreferences.getInstance().then((prefs) {
        prefs.setString("auth", sr.data);
        prefs.setString("phone", myPhone);
    }
    

    () => {} requires comma

    SharedPreferences.getInstance().then((prefs) => {
        prefs.setString("auth", sr.data),
        prefs.setString("phone", myPhone),
    }
    
    Login or Signup to reply.
  2. The solution doesn’t actually give the answer, and @noone392 in its comment does not know what he’s talking about.

    () {} is a function, whose code exists inside of {}.
    () => {} is also a function, but one that return an empty Map object.

    This is the code that OP wants (notice the lack of =>):

    SharedPreferences.getInstance().then((prefs) {
        prefs.setString("auth", sr.data);
        prefs.setString("phone", myPhone);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search