I'm building an application in flutter/dart and I'm trying to check if the returned route information is empty before proceeding with the rest of the route building, I'm doing that inside the "factory", and if isEmpty I'm returning null however, I'm getting the following error "A value of type 'Null' can't be returned from the constructor 'Directions.fromMap' because it has a return type of 'Directions'" Thanks for your support
class Directions{
final LatLngBounds bounds;
final List<PointLatLng> plylinePoints;
final String totalDistance;
final String totalDuration;
const Directions ({
required this.bounds, required this.plylinePoints,
required this.totalDistance, required this.totalDuration
});
factory Directions.fromMap(Map<String, dynamic> map) {
if ((map['routes'] as List).isEmpty) return null;
CodePudding user response:
The factory
constructor in Dart only allow you to return an instance of the class or of a subclass. You can't return null
from it.
What you could do instead is to use a static Directions? fromMap(Map<String, dynamic> map)
function.
That way you would be able to return either a Direction
or null