PacktPublishing/Flutter-Cookbook

related to null safety?

Closed this issue · 2 comments

For the pizza json chapter 8 I believe with a factory constructor, null cannot be returned. Please update the code. I have no idea what purpose the factory serves now except if there is a cache of instantiated pizzas in the pizza class which then could be used to optimize a bit by not having to instantiate every instance. But I am unclear on the "how" this works under the hood.

I believe that now due to null-safety this should be migrated to be a static method that returns null, which would then allow it to maintain it's intended functionality.

factory Pizza.fromJsonOrNull(Map<String, dynamic> json)

would become

static Pizza? fromJsonOrNull(Map<String, dynamic> json)

Please see this documentation - https://dart.dev/null-safety/faq#how-should-i-migrate-a-factory-that-can-return-null

Thank you @luketg8 for the solution