Home > OS >  error: 1 positional argument(s) expected, but 0 found
error: 1 positional argument(s) expected, but 0 found

Time:04-30

When I want to declare this function in my main.dart

class MapEqualColorMappingPage extends SampleView {
  const MapEqualColorMappingPage(Key key) : super(key: key);

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

class _MapEqualColorMappingPageState extends SampleViewState {
   List<_CountryTimeInGMT> _timeZones;
   MapShapeSource _mapSource;

...
}

i get the following error

error: 1 positional argument(s) expected, but 0 found.

     '/map': (context) => MapEqualColorMappingPage(),  //The error is HERE

what should id ?

CodePudding user response:

Instead of MapEqualColorMappingPage(Key key) write MapEqualColorMappingPage({Key? key})

class MapEqualColorMappingPage extends SampleView {

const MapEqualColorMappingPage({Key? key}) : super(key: key);

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

class _MapEqualColorMappingPageState extends SampleViewState {
   List<_CountryTimeInGMT> _timeZones;
   MapShapeSource _mapSource;

...
}

CodePudding user response:

Here

const MapEqualColorMappingPage(Key key) : super(key: key);

you are stating that the constructor must have an argument of type Key, and here

'/map': (context) => MapEqualColorMappingPage()

you're trying to use the same constructor without any kind of argument. You can:

  • add a Key argument when you use the constructor
  • remove the Key argument from the constructor declaration
  • put the Key argument in the declaration in curly braces in order to make it optional

CodePudding user response:

You are not passing Key parameter.

const MapEqualColorMappingPage(Key? key) : super(key: key);

Mark it as optional Key?

  • Related