【问题标题】:Flutter Amplify DataStore plugin has not been added to AmplifyFlutter Amplify DataStore 插件尚未添加到 Amplify
【发布时间】:2022-01-18 19:41:28
【问题描述】:

突然间,我收到错误 DataStore plugin has not been added to Amplify, recoverySuggestion: Add DataStore plugin to Amplify and call configure before calling DataStore related APIs 以排除我在该页面上所做的任何工作,我在新页面上尝试了相同的结果。

我已经执行了amplify codegen modelsamplify pullamplify env pull。还尝试做一个flutter clean,但我根本看不到任何变化。我真的很困惑,似乎无法弄清楚问题所在。

我在调试时注意到的一件事是屏幕的initState 似乎作为configureAmplify 回调执行得更早。

我会展示代码的相关部分(抱歉,代码太长了)。

Pubspec.yaml

dependencies:
  ...
  amplify_flutter: ^0.2.10
  amplify_datastore: ^0.2.10
  amplify_api: ^0.2.10
  amplify_auth_cognito: ^0.2.10
  amplify_storage_s3: ^0.2.10

main.dart

import 'package:flutter/material.dart';
import 'package:my_package/screens/main/teams_screen.dart';
import 'package:my_package/services/amplify_services.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    AmplifyService.configureAmplify();
  }

  @override
  Widget build(BuildContext context) {
  ...
  }
}

服务/amplify_services.dart

import 'package:flutter/foundation.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_datastore/amplify_datastore.dart';
import 'package:amplify_api/amplify_api.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_storage_s3/amplify_storage_s3.dart';
import 'package:my_package/models/ModelProvider.dart';
import 'package:my_package/amplifyconfiguration.dart';

class AmplifyService {
  static configureAmplify() async {
    AmplifyAPI apiPlugin = AmplifyAPI();
    AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
    AmplifyStorageS3 amplifyStorageS3 = AmplifyStorageS3();
    AmplifyDataStore dataStorePlugin = AmplifyDataStore(
      modelProvider: ModelProvider.instance,
    );

    await Amplify.addPlugins([
      dataStorePlugin,
      authPlugin,
      amplifyStorageS3,
      apiPlugin,
    ]);

    try {
      await Amplify.configure(amplifyconfig);
    } on AmplifyAlreadyConfiguredException {
      if (kDebugMode) {
        print(
            "Amplify was already configured. Looks like app restarted on android.");
      }
    }
  }
}

最后是非常基本的页面,甚至没有输出 (screens/teams_screen.dart)

import 'dart:async';
import 'package:amplify_datastore/amplify_datastore.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:flutter/material.dart';
import 'package:my_package/models/Team.dart';

class TeamsScreen extends StatefulWidget {
  const TeamsScreen({Key? key}) : super(key: key);

  @override
  _TeamsScreenState createState() => _TeamsScreenState();
}

class _TeamsScreenState extends State<TeamsScreen> {
  late StreamSubscription<QuerySnapshot<Team>> _teamsSubscription;
  bool _isLoading = true;
  List<Team> teams = [];

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

  @override
  void dispose() {
    _teamsSubscription.cancel();
    super.dispose();
  }

  Future<void> _initializeApp() async {
    _teamsSubscription = Amplify.DataStore.observeQuery(Team.classType)
        .listen((QuerySnapshot<Team> snapshot) {
      setState(() {
        if (_isLoading) _isLoading = false;
        teams = snapshot.items;
      });
    });
  }

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

【问题讨论】:

    标签: flutter aws-amplify flutteramplify


    【解决方案1】:

    新的一天,新的头脑。问题很简单,我没有设置_isLoading 状态来指示是否潮湿,configureAmplify 回调已完成,让应用程序继续加载所有其他触发错误的屏幕。因此,在设置状态并仅在状态更改后添加应用程序的其余部分后,它可以正常工作。

    为了修复它,我做了以下操作:

    import 'package:flutter/material.dart';
    import 'package:my_package/screens/main/teams_screen.dart';
    import 'package:my_package/services/amplify_services.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        super.initState();
        _initializeApp();
      }
    
      Future<void> _initializeApp() async {
        await AmplifyService.configureAmplify(); // note the await!
    
        setState(() {
          _isLoading = false; // important to set the state!
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: _isLoading 
              ? Center(child: CircularProgressIndicator())
              : const MainScreen(), // _isLoading is very important here.
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2022-06-18
      • 2022-12-06
      • 2021-11-17
      • 2021-01-30
      • 2022-01-16
      相关资源
      最近更新 更多