方法 'Amplify' 没有为类型“_MyAppState”定义。 -FlutterAmplify

The method 'Amplify' isn't defined for the type '_MyAppState'. -FlutterAmplify

我刚刚开始学习 AWS Amplify,并按照 https://aws.amazon.com/getting-started/hands-on/build-flutter-app-amplify/module-two/ 中给出的步骤在我的 flutter 项目中初始化 Amplify。但是当我尝试集成 Amplify(使用语句 final _amplify = Amplify();) 在我的应用程序中。

下面是我的 main.dart 文件。

import 'package:flutter/material.dart';
import './amplifyconfiguration.dart';
import 'package:amplify_core/amplify_core.dart';

void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  final _amplify = Amplify();

  @override
  void initState() {
    super.initState();
    _configureAmplify();
  }

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

  void _configureAmplify() async {
    try {
      await _amplify.configure(amplifyconfig);
      print('Successfully configured Amplify ');
    } catch (e) {
      print('Could not configure Amplify ☠️');
    }
  }
}

放大我在 pubspec.yaml

中导入的依赖项
amplify_core: '<1.0.0'

amplifyconfiguration.dart

const amplifyconfig = ''' {
   "UserAgent": "aws-amplify-cli/2.0",
   "Version": "1.0"
}''';

另外,当我 运行“放大控制台”时,我得到 App with id: xxxxxxxxxx not found

根据https://github.com/aws-amplify/amplify-flutter/issues/274 amplify_core 现在重命名为 amplify_flutter 因此您需要更改

amplify_core: '<1.0.0'

amplify_flutter: '<1.0.0'

另外,Amplify class现在是静态单例,不需要实例化,所以可以去掉这一行:

final _amplify = Amplify();