6

I want to create a generic public method navigate the application to login screen if any response is: unauthenticated.

The problem is I don't have the context to use it in my Navigator.

In Android I used to use: getApplicationContext(), how can I do that in Flutter?

Abdurrahman Anas
  • 665
  • 2
  • 9
  • 21

3 Answers3

1

It will work as context below method is a custom method

 Widget profileTextView(BuildContext context) {  
      }

Happy New Year :)

Aamil Silawat
  • 7,735
  • 3
  • 19
  • 37
  • 2
    I used something like that, but it still not equivalent to "getApplicationContext()", it's another way to apply this method. Flutter don't have a method does that? – Abdurrahman Anas Dec 31 '19 at 12:53
0

Using flutter_modular packages you can manage your application status, routes and many other things. But what interests us in this case is the context in the entire application with the following command: Modular.to.navigator.context

Remembering that for routes, using this package you don't need the context, but if you need it anywhere this command above solves the problem.

Edeson Bizerril
  • 1,595
  • 1
  • 11
  • 12
-2

In Dart, we do not often use getter and setters. In Java you are using getApplicationContext() for getting the Context. In flutter you can directly use the predefined Context variable anywhere inside the app as follows:

@override
Widget build(BuildContext context) {
    return Container();
} 

BuildContext we can use as the Application Context.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
Guru Prasad mohapatra
  • 1,809
  • 13
  • 19