Home > Software design >  How to pass two lists to another screen in flutter
How to pass two lists to another screen in flutter

Time:12-30

I want to pass to lists which are destinations and coordinates. I had to separate them because I have to scrape the data first then geocode them. Here is my code:

Future<void> getCoordinates()async{
for(int i=1 ; i < destination.length ; i  ){
  coordinates = await locationFromAddress('${destination.elementAt(i).destination}');
  location.add(coordinates);
  print('$i $location');
}

}

Heres a snippet to go from my first screen to second screen:

return GestureDetector(
                onTap: (){
                  Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => PlaceInfo(packageModel: destination[index], location: location[index])),
                  );
                },

Here is a snippet of my second screen:

  final PackageModel packageModel;
  final Location location;
  PlaceInfo({Key? key,required this.location, required this.packageModel,}):super(key:key);

  @override
  State<PlaceInfo> createState() => _PlaceInfoState();
}

class _PlaceInfoState extends State<PlaceInfo> {

  //late double _latitude = 2.8025;
  //late double _longitude = 101.7989;

  late double _latitude = widget.location.latitude;
  late double _longitude = widget.location.longitude;

When I do this it shows in the app the red error which is

type 'List<Location>' is not a subtype of type 'Location'

Can somebody please help

CodePudding user response:

May be as per your error in my opinion one potential solution is when you are passing arguments to next screen you need to change the type of the argument same as below

 Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => PlaceInfo(packageModel: destination[index], location: (location[index] as Location))),
                  );

You just need to add as Location after the location[index]

And another solution is you can declare your List location variable as

List<Location> location.....

CodePudding user response:

The error lies in this: location.add(coordinates);.

It appears that coordinates itself is a List and you are trying to add it as an element.

Try to first create a Location 0bject from coordinates and then add that object to the list location.

Hope it helps!

  • Related