0
votes

So, I have my lab_widget that has a normal button and it calls a function from another file called not_plugin. I want that every time I click on the button, the function in not_pugin opens a determined screen. This is what I have in not_plugin:


Future<dynamic> onClick(String payload) async {
     //Do other things

     Navigator.push(context, MaterialPageRoute(builder: (context) => LabMdScreen()));
}

But it throws the following error:

46:24: Error: The getter 'context' isn't defined for the class 'NotPlugin'.

  • 'NotPlugin' is from 'not_plugin.dart'. Try correcting the name to the name of an existing getter, or defining a getter or field named 'context'. Navigator.push(context, MaterialPageRoute(builder: (context) => MyScreen()));

So how can I resolve this?

UPDATE

After pass a BuildContext, throws this error in another function:

Future<void> initLocalNotification() async {
    androidInitializationSettings =
        AndroidInitializationSettings('@mipmap/ic_launcher');//here
    iosInitializationSettings = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    initializationSettings = InitializationSettings(
        android: androidInitializationSettings, iOS: iosInitializationSettings);

    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onClick());
  }

In the last line (onSelectNotification: onClick());)

error: The argument type 'Future' can't be assigned to the parameter type 'Future Function(String)'

1

1 Answers

0
votes

You need to pass a BuildContext to your function:

void onClick(String payload, BuildContext context) {
     //Do other things

     Navigator.push(context, MaterialPageRoute(builder: (context) => LabMdScreen()));
}