Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK Error #39078

Closed
rhymelph opened this issue Oct 24, 2019 · 3 comments
Closed

SDK Error #39078

rhymelph opened this issue Oct 24, 2019 · 3 comments

Comments

@rhymelph
Copy link

SDK version

2.5.0

You can use this code and run it.

void main(){
 Test test=Test.fromMap(null);
//throw Exception
}

class Test{
 String id;
 String name;
 Test();

 factory Test.fromMap(Map map)=>map==null?Test():Test()
 ..id=map["id"]
 ..name=map["name"];
}

result:

E/flutter ( 7677): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 7677): Receiver: null
E/flutter ( 7677): Tried calling: []("id")
....
@eernstg
Copy link
Member

eernstg commented Oct 24, 2019

Try this:

void main() {
  Test test = Test.fromMap(null);
//throw Exception
}

class Test {
  String id;
  String name;
  Test();

  factory Test.fromMap(Map map) => map == null ? Test() : (Test()
    ..id = map["id"]
    ..name = map["name"]);
}

The problem is that you are using a cascade whose receiver (that is, e in an expression e..s where s is a <cascadeSection>) is not the object obtained from Test() but rather the one from map == null ? Test() : Test().

It is a known source of confusion that cascades have such a low precedence. However, if we want to allow the right hand side of a cascade assignment to contain a wide assortment of expressions then the precedence has to be low. In particular, with e1..x = e2, e2 must be an <expressionWithoutCascade>, and that's nearly any expression.

@rhymelph
Copy link
Author

@eernstg thank.

@eernstg
Copy link
Member

eernstg commented Oct 24, 2019

Thanks! For prior discussions about the precence of the cascade syntax, please follow #608 (that's an old issue which was moved here from the SDK repository, so it's much older than the number 608 would imply).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants