dartsdkflutter2.0

Flutter 2.0: The return Type <List<LatLng>> isn’t a 'void', as required by the closure's context


I am getting the error The return Type <List<LatLng>> isn’t a 'void', as required by the closure's context regarding the line return polygonList; in this button widget:

Widget _markerButton(BuildContext context) {
  return IconButton(
    icon: Icon(Icons.favorite, color: _list ? Colors.green : Colors.red),
    onPressed: () {
      setState(() {
        _list = !_list;
        numberOfMarkers(latlng)[0];
        realtyTap = true;
      });
      return polygonList;
    },
  );
}

It worked excellent before. This error appeared when I switched to Flutter 2.0 and sdk: "> = 2.12.0 <3.0.0” .

I looked at the answers to questions from people with similar difficulties, but it did not help me.

The whole page code is:

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';


class HomePage extends StatefulWidget {
  final Function state;
  HomePage(this.state);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late MapController _mapController;
  late LatLng latlng;

  List<Marker> markers = [];
  List<LatLng> polygonList = [];

  bool singleTap = false;
  bool _list = false;

  double maxZoom = 6.0;
  double minZoom = 4.0;

  @override
  void initState() {
    super.initState();
  }

  Widget _markerButton(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.favorite, color: _list ? Colors.green : Colors.red),
      onPressed: () {
        setState(() {
          _list = !_list;
        });
        return polygonList;
      },
    );
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: FlutterMap(
        mapController: _mapController,
        options: MapOptions(
            center: LatLng(40.000000, 70.000000),
            zoom: 4,
            onTap: (latlng) {
              if (singleTap) {
                setState(() {
                  if (markers.length == 0) {
                    markers.add(Marker(
                        point: latlng,
                        builder: (context) => _markerButton(context)));
                  } else {
                    markers.add(
                      Marker(
                        point: latlng,
                        builder: (ctx) => const Icon(
                          Icons.fiber_manual_record_rounded,
                          color: Colors.red,
                          size: 15.0,
                        ),
                      ),
                    );
                  }
                  polygonList.add(latlng);
                });
              }
            }),
        layers: [
          TileLayerOptions(
              urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
          PolygonLayerOptions(
            polygons: [
              Polygon(
                points: polygonList,
                borderStrokeWidth: 3.0,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

What should be happened here: if the user clicks for the second time the point from which s/he started building the polygon on the map, the button change its color, the building stops, and a list of points in the corners of the constructed polygon is returned.

I would be grateful if you could give me some advice on how to fix this issue.


Solution

  • Dart 2.12 added stricter type checking to catch cases where callbacks return unused values.

    IconButton.onPressed is a VoidCallback?; that is, if it isn't null, it is a function that returns nothing.

    You are supplying an anonymous function that has return polygonList; in it. That returned value can never be used, so the return statement is useless. Simply remove it.