【问题标题】:Calling a dart method in another dart file from main.dart从 main.dart 调用另一个 dart 文件中的 dart 方法
【发布时间】:2021-03-20 20:56:51
【问题描述】:

我正在尝试从 main 调用另一个 dart 文件中的 dart 方法。镖。由于某种原因,该方法似乎无法访问。在这里,我试图访问另一个类中的 _configureAmplify 方法。我正确添加了所有导入。我究竟做错了什么? 这是我的 main.dart 类 -

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;
      }
    });

  }
}

这是另一个定义 _configureAmplify 方法的类

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;
      }
    }

编辑

【问题讨论】:

  • 从 _configureAmplify 中删除下划线 .. 因为下划线用于使其私有

标签: flutter dart


【解决方案1】:

在 Dart 中,当您使用 _ 执行函数时,它会将其设为私有,即。无法从课堂外访问。

如果该函数仅用于在类中使用,最好将其保持为私有。但是如果你需要从外部访问它,那么它需要是公开的,没有下划线。

【讨论】:

  • 当我在 main.dart 文件中使用一个点进行身份验证时,智能感知并没有给我选择 configureAmplify 的选项。当我手动插入时,方法名称下会出现红色波浪线。
  • 我已经用图片编辑了原帖
  • 在身份验证后添加() 或创建Authentication 对象final authentication = Authentication(); 然后使用authentication.configureAmplify();
  • 新手来扑腾和飞镖。谢谢 Loren.A
猜你喜欢
  • 2021-04-12
  • 2020-08-25
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 2020-12-03
  • 2019-09-09
  • 2021-12-03
相关资源
最近更新 更多