简体   繁体   English

冻结 package flutter 在代码生成中抛出不可为空的错误

[英]Freezed package flutter throwing non-nullable error in code generation


I am starting a project and decided to use the new version version of **Freezed** for my models, but when I run flutter *pub run build_runner build* to generate my code I get the following error: 我正在开始一个项目并决定为我的模型使用新版本的 **Freezed**,但是当我运行 flutter *pub run build_runner build* 来生成我的代码时,我收到以下错误:
>The parameter 'placeFormattedAddress' of 'Address' is non-nullbale but is neither required nor marked with @Default > 'Address' 的参数 'placeFormattedAddress' 是非空包,但既不是必需的,也不是用@Default 标记的
> I am importing the **Meta** package and also including the *@required* annotation; > 我正在导入 **Meta** package 并且还包括 *@required* 注释; the funny thing is that when I change the parameters from named to positional (without the curly braces, it works well). 有趣的是,当我将参数从命名更改为位置时(没有花括号,效果很好)。
Here is the code ¿Could anybody please point me what is wrong? 这是代码¿有人可以指出我有什么问题吗?
**Here is the class** **这是课程**
 import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:flutter/foundation.dart'; part 'address.freezed.dart'; part 'address.g.dart'; @freezed class Address with _$Address { const factory Address({ @required String placeFormattedAddress, @required String placeName, @required String placeId,@required double latitude, @required double longitude,}) = _Address; factory Address.fromJson(Map<String, dynamic> json) =>_$AddressFromJson(json);}

Here is my pubspec.yaml这是我的 pubspec.yaml

 publish_to: 'none' version: 1.0.0+1 environment: sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter freezed_annotation: ^0.14.1 json_serializable: ^4.1.0 flutter_hooks: ^0.16.0 hooks_riverpod: ^0.13.1 dev_dependencies: flutter_test: sdk: flutter lint: ^1.0.0 build_runner: freezed: ^0.14.1+2

I finally found the answer;我终于找到了答案; with null-safe dart the @ is no longer needed to mark a required parameter;使用 null-safe dart 不再需要 @ 来标记必需的参数; so instead of @required , now the syntax is only required所以现在只需要语法而不是@required

When null safe code is called from legacy code the required keyword is treated exactly like the @required annotation: failure to supply the argument will cause an analyzer hint.当从遗留代码调用 null 安全代码时,所需关键字的处理方式与 @required 注释完全相同:未能提供参数将导致分析器提示。

https://dart.dev/null-safety/faq https://dart.dev/null-safety/faq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 颤振不可为空 - Flutter non-nullable Flutter 不可为空的实例字段错误 - Flutter Non-nullable instance Field error Flutter 错误 -> 必须初始化不可为 null 的“项目” - Flutter Error -> Non-nullable 'items' must be initialized 在颤振中使用不可为空后缓存的网络图像抛出错误 - Cached network image is throwing errors after using non-nullable in flutter Flutter SharedPreferences 不可为空的实例字段 - Flutter SharedPreferences non-nullable instance fields 错误:这需要启用“不可为空”实验 - Error: This requires the 'non-nullable' experiment to be enabled Flutter 制作单例类并获得不可为空的实例字段错误 - Flutter making singleton class and getting Non-nullable instance field error 如何解决 Flutter/Dart 中的“不可为空的实例字段 &#39;_controller&#39; 必须初始化”错误? - How to resolve "Non-nullable instance field '_controller' must be initialized" error in Flutter/Dart? Flutter 错误:主体可能正常完成,导致返回“null”,但返回类型可能是不可为空的类型 - Flutter Error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type 不可为 null 的局部变量“title”必须先赋值,然后才能使用。 Flutter 错误 - The non-nullable local variable 'title' must be assigned before it can be used. Flutter error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM