0
votes

I am trying to call a dart method which is in another dart file from main. dart. For some reason the method seems to inaccessible. Here I am trying to access the _configureAmplify method which is in another class. I got all the import added correctly. What am I doing wrong? Here is my main.dart class -

class _AmplifyFlutterState extends State<AmplifyFlutter> {
  bool _amplifyConfigured = false;


  void initState() {
    super.initState();
    Authentication._configureAmplify().then((result) {
      if (result) {
        setState(() {
          _amplifyConfigured = true;
          Navigator.push(context, MaterialPageRoute(builder: (_) => SignUpScreen()));
        });
      } else {
        _amplifyConfigured = false;
      }
    });

  }
}

Here is the other class which defines the _configureAmplify method

class Authentication {
    
      Future<bool> _configureAmplify() async {
        bool _amplifyConfigured = false;
        Amplify.addPlugin(AmplifyAuthCognito());
        try {
          await Amplify.configure(amplifyconfig);
        }
        on AmplifyAlreadyConfiguredException {
          print("Amplify was already configured. Was the app restarted?");
        } catch(e) {
          print(e);
        }
        return _amplifyConfigured;
      }
    }

EDIT enter image description here

1
Remove the underscore from _configureAmplify .. as the underscore are use to make it private - thenoobslayer

1 Answers

1
votes

In Dart, when you proceed a function with _ it makes it private ie. inaccessible from outside the class.

If the function is only meant to be used from within the class, it's always better to keep it private. But if you need to access it from outside, then it needs to be public with no underscore.