1

我正在尝试将 Amplify Api 连接到我的项目并不断收到错误消息LateError (LateInitializationError: Field 'customTypeSchemas' has not been initialized.)(在代码片段之后提供了错误的屏幕截图),我认为这意味着在 Amplify 提供给我的代码中,某处有一行带有late前缀对于customTypeSchemas字段,我找不到。

这是我初始化 Amplify 的地方。

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

void _configureAmplify() async {
  if (!Amplify.isConfigured) {
    await Amplify.addPlugins([
      AmplifyDataStore(modelProvider: ModelProvider.instance),
      AmplifyAPI(),
    ]);

    try {
      await Amplify.configure(amplifyconfig);
    } catch (e) {
      print(e.toString());
    }
  }
}

错误截图

这是我的pubspec.yaml依赖项

dependencies:
  amplify_api: ^0.3.2
  amplify_datastore: ^0.3.2
  amplify_flutter: ^0.3.2
  bloc: ^8.0.2
  cupertino_icons: ^1.0.2
  flutter:
    sdk: flutter
  flutter_bloc: ^8.0.1

这是ModelProvider.dartAmplify 下载的内容。

/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*  http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

// ignore_for_file: public_member_api_docs

import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';

import 'Todo.dart';

export 'Todo.dart';

class ModelProvider extends ModelProviderInterface {
  @override
  String version = "d960b875068d46ff04d68879d96cc042";
  @override
  List<ModelSchema> modelSchemas = [Todo.schema];
  static final ModelProvider _instance = ModelProvider();

  static ModelProvider get instance => _instance;

  @override
  ModelType getModelTypeByModelName(String modelName) {
    switch (modelName) {
      case "Todo":
        {
          return Todo.classType;
        }
      default:
        {
          throw Exception("Failed to find model in model provider for model name: " + modelName);
        }
    }
  }
}

我确实不小心安装了amplify_core: ^0.3.2,我删除并运行了它,flutter clean并且在 ios 文件夹中pod cache clean --all

重现问题的步骤:

  1. 创建颤振项目
  2. 将 Amplify 添加到您的项目中
  3. 添加数据库插件
  4. 将 API 添加到您的项目中
  5. 尝试添加 api 插件并在 iOS 模拟器中运行该应用程序。

我对编程和 StackOverflow 比较陌生,所以请原谅我在这篇文章中可能犯的任何错误。如果您需要更多信息或想尝试一下,请告诉我。

提前致谢。

4

3 回答 3

1

_configureAmplify是一种未来的方法,使用FutureBuilder. 您还可以在使用模型时使其为空并进行空检查。

你可以检查我什么时候应该使用 FutureBuilder?

示例代码:检查上面的链接。

FutureBuilder(
    future: _configureAmplify(), // async work
    builder: (BuildContext context, snapshot) {
       switch (snapshot.connectionState) {
         case ConnectionState.waiting: return Text('Loading....');
         default:
           if (snapshot.hasError)
              return Text('Error: ${snapshot.error}');
           else
          return Text('Result:}');
        }
      },
    )
于 2022-01-27T05:55:27.343 回答
0

我最近在升级到颤振 2.10 后观察到该错误

我能够通过将以下覆盖方法添加到我的模型提供程序类来修复它

  @override
    List<ModelSchema> customTypeSchemas = [];

添加后,您的模型应如下所示

   /*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*  http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

// NOTE: This file is generated and may not follow lint rules defined in your app
// Generated files can be excluded from analysis in analysis_options.yaml
// For more info, see: https://dart.dev/guides/language/analysis-options#excluding-code-from-analysis

// ignore_for_file: public_member_api_docs, file_names, unnecessary_new, prefer_if_null_operators, prefer_const_constructors, slash_for_doc_comments, annotate_overrides, non_constant_identifier_names, unnecessary_string_interpolations, prefer_adjacent_string_concatenation, unnecessary_const, dead_code

import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
import 'Todo.dart';

export 'Todo.dart';

class ModelProvider implements ModelProviderInterface {
  @override
  String version = "d960b875068d46ff04d68879d96cc042";
  @override
  List<ModelSchema> modelSchemas = [Todo.schema];
  static final ModelProvider _instance = ModelProvider();

  static ModelProvider get instance => _instance;

  ModelType getModelTypeByModelName(String modelName) {
    switch (modelName) {
      case "Todo":
        {
          return Todo.classType;
        }
        break;
      default:
        {
          throw Exception(
              "Failed to find model in model provider for model name: " +
                  modelName);
        }
    }
  }

  @override
  List<ModelSchema> customTypeSchemas = [];
}


于 2022-02-08T05:40:28.683 回答
0

你可以试试他的

@override
List<ModelSchema> customTypeSchemas = [];

确保在您的ModelProviderClass

于 2022-02-22T11:11:17.267 回答