Flutter Geo location error in Address and App Data file

Viewed 331

Used Flutter 2.2.2 Android studio - 4.2.1

This is my appData.dart file code have error in class address.have issue in save location.

    import 'package:flutter/cupertino.dart';
    import 'package:riderapp/Models/address.dart';
    
    class AppData extends ChangeNotifier
    {
       Address pickUpLocation;
    
      void updatePickUpLocationAddress(Address pickUpAddress)
      {
        pickUpLocation = pickUpAddress;
        notifyListeners();
      }
    }

This is my address.dart file have error in address line

    class Address
    {
      String placeFormattedAddress;
      String placeName;
      String placeId;
      double latitude;
      double longitude;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }
2 Answers

From my experience, this comes from the new null-safety features introduced in dart 2.0. I would recommend checking the official dev docs for more information.

You have two options.

1: Make them all nullable using ?

Therefore, your code will look like (only address.dart)

    class Address
    {
      String? placeFormattedAddress;
      String? placeName;
      String? placeId;
      double? latitude;
      double? longitude;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }

However this may lead to more issues in the future regarding null-safety checks.

2: Initialize them with a default value to prevent initialization being null.

    class Address
    {
      String placeFormattedAddress = '';
      String placeName = '';
      String placeId = '';
      double latitude = 0.0;
      double longitude = 0.0;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }

Just add

Address pickUpLocation = Address(placeFormattedAdress:'',placeName:'',placeId:'',latitude:0.0,longitude:0.0);

This actually solved my issue and the code run perfectly.

Related