0

I'm stuck on this, I have no clue why won't the last print execute past object value initialization.

await _client
      .get(Uri.parse(_url), headers: {"location": "Mars"})
      .then((result) => result.body)
      .then(json.decode)
      .then((json) => json.forEach((person) {
        print(person); // this gets executed and printed over and over
        Person newPerson;
        print('hehe lolz'); // this gets executed too
        newPerson.status = person['status'];
        print('hello'); // this never gets executed...

Person class is just a model with all String fields and a constructor.

What could be blocking the execution of last print?

1

That's because newPerson is null and when you are trying to call newPerson.status - the NullPointer exception appears and all the code below doesn't execute. Just make:

Person newPerson = Person();
  • I wanted to argue that I tried with named constructor from my model before and it wouldn't work - but I tried this and it worked like a charm. Thanks man. – Alan Kałuża Apr 11 at 15:22
  • 1
    Some times magic happens :) – WorieN Apr 11 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.