'2 positional argument(s) expected, but 0 found when calling Class Flutter

I am trying to reach another Class in Flutter.

I have made the Class an object in my main.dart

I have am then trying to call it and having issues.

I receive the error 2 positional argument(s) expected, but 0 found.. This is becuase in the Class I define HereMapController & ShowDialogFunction2. I can't work out how to link them. Any help appreciated.

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
} 

class _MyAppState extends State<MyApp> {
  HereMapController? hereMapController;
  ShowDialogFunction2? showDialogCallback2;



Future<void> intermodalRouter() async{
...
       final MapItemsExample2 _mapItemsExample2 = MapItemsExample2();        //Issue Here

       _sendIntermodalDataToSecondScreen(context, mappedValues, dest);     //Works Fine
       _mapItemsExample2.showFirstPolylineMapMarkers(deplat, deplong, deplocname);      //This is trying to reach other Class
      }
    });
  }
typedef ShowDialogFunction2 = void Function(RichText title, RichText message);

class MapItemsExample2 {
  final ShowDialogFunction2 _showDialog2;
  final HereMapController _hereMapController;

 MapItemsExample2(ShowDialogFunction2 showDialogCallback2, HereMapController hereMapController)
      : _showDialog2 = showDialogCallback2,
        _hereMapController = hereMapController {
...

When I hover over the issue line it defines Class MapItemsExample2 as

(new) MapItemsExample2 MapItemsExample2(void Function(RichText, RichText) showDialogCallback2, HereMapController hereMapController)

I changed it to the following and no errors show. Then on running it calls null. I know i have set this up wrong but I just need to be able to reach the other Class

       final MapItemsExample2 _mapItemsExample2 = MapItemsExample2(showDialogCallback2!, hereMapController!);

Thank you



Solution 1:[1]

you don't have a constructor for the class MapItemsExample2 with not arguments, since you have added a constructer with two arguments you cannot use the default constructor. The solution would be to create a constructer with no arguments in addition to to your "two arguments" constructor and make your variables late if you don't want them initialized in the second constructor like:

class MapItemsExample2 {
  late ShowDialogFunction2 _showDialog2;
  late HereMapController _hereMapController;

 MapItemsExample2(ShowDialogFunction2 showDialogCallback2, HereMapController hereMapController)
      : _showDialog2 = showDialogCallback2,
        _hereMapController = hereMapController {}
    
MapItemsExample2.empty()
{
//this is the second empty constructor
}
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1