【问题标题】:LateError (LateInitializationError: Field 'customTypeSchemas' has not been initialized.) Amplify Plugins ErrorLateError(LateInitializationError:字段'customTypeSchemas'尚未初始化。)放大插件错误
【发布时间】:2022-02-23 01:50:42
【问题描述】:

我正在尝试将 Amplify Api 连接到我的项目并不断收到错误消息 LateError (LateInitializationError: Field 'customTypeSchemas' has not been initialized.)(错误的屏幕截图在代码 sn-p 之后提供)我认为这意味着在 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());
    }
  }
}

Screenshot of the error

这是我的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

这是 Amplify 下载的 ModelProvider.dart 中的内容。

/*
* 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 比较陌生,所以请原谅我在这篇文章中可能犯的任何错误。如果您需要更多信息或想尝试一下,请告诉我。

提前致谢。

【问题讨论】:

    标签: flutter aws-amplify amplify aws-amplify-cli amplify-flutter


    【解决方案1】:

    你可以试试他的

    @override
    List<ModelSchema> customTypeSchemas = [];
    

    确保在 ModelProviderClass 中初始化一个空列表

    【讨论】:

      【解决方案2】:

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

      您可以查看When should I use a 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:}');
              }
            },
          )
      

      【讨论】:

      • 我仍然遇到同样的错误。
      • 当前错误是什么
      • 同样的错误LateError (LateInitializationError: Field 'customTypeSchemas' has not been initialized.)
      • 你能告诉我customTypeSchemas在哪里
      • 这就是我感到困惑的地方,因为当我进行搜索时,我看到很多包含这个变量的文件,但不知道哪个文件失败了。如果有帮助,我从 Amplify 下载了 ModelProvider。
      【解决方案3】:

      我最近在升级到 Flutter 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 = [];
      }
      
      
      

      【讨论】:

      • 我刚刚在我的项目中删除了与 AWS 相关的所有内容,然后重新开始,它确实有效,我没有花太多时间在它上面,所以这没什么大不了的。谢谢你的回复。
      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 2021-10-27
      • 2021-12-29
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多