必须初始化不可为 null 的实例字段“_selectedSize”
Non-nullable instance field '_selectedSize' must be initialized
我一直在使用我的商店应用程序,但现在这种空安全让我很生气。我已经创建了一个 class 但它给了我这个错误,后来不允许我的应用程序正常工作
这是 product.dart 文件:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:loja_virtual_nnananene/models/item_size.dart';
class Product extends ChangeNotifier {
Product.fromDocument(DocumentSnapshot document) {
id = document.documentID;
name = document['name'];
description = document['description'];
images = List<String>.from(document.data['images'] as List<dynamic>);
// ingore_for_file: Warning: Operand of null-aware operation
sizes = (document.data['sizes'] as List<dynamic> ?? [])
.map((s) => ItemSize.fromMap(s as Map<String, dynamic>))
.toList();
}
String id = "";
String name = "";
String description = "";
List<String> images = [];
List<ItemSize> sizes = [];
ItemSize _selectedSize;
ItemSize get selectedSize => _selectedSize;
set selectedSize(ItemSize value) {
_selectedSize = value;
notifyListeners();
}
}
我在 Product.from...
这是错误:
Non-nullable instance field '_selectedSize' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
这是我的 ItemSize class:
class ItemSize {
ItemSize.fromMap(Map<String, dynamic> map) {
name = map['name'] as String;
price = map['price'] as num;
stock = map['stock'] as int;
}
String name = "";
num price = 0;
int stock = 0;
bool get hasStock => stock > 0;
@override
String toString() {
return 'ItemSize{name: $name, price: $price, stock: $stock}';
}
}
调用主窗口小部件:
class SizeWidget extends StatelessWidget {
const SizeWidget(this.size);
final ItemSize size;
@override
Widget build(BuildContext context) {
final product = context.watch<Product>();
final selected = size == product.selectedSize;
Color color;
if (!size.hasStock)
color = Colors.red.withAlpha(50);
else if (selected)
color = ColorSelect.cprice;
代码第一次尝试获取选中项,当然会为null。我找到的替代方案是...
在 ItemSize class 中,我创建了一个简单的构造函数,所有 null -> ItemSize();
class ItemSize {
ItemSize.fromMap(Map<String, dynamic> map) {
name = map['name'] as String;
price = map['price'] as num;
stock = map['stock'] as int;
}
ItemSize();
String? name;
num? price;
int? stock;
bool get hasStock => stock! > 0;
@override
String toString() {
return 'ItemSize{name: $name, price: $price, stock: $stock}';
}
}
在产品中 class 以这种方式获取。
ItemSize get selectedSize {
if (_selectedSize != null)
return _selectedSize!;
else
return ItemSize();
}
我一直在使用我的商店应用程序,但现在这种空安全让我很生气。我已经创建了一个 class 但它给了我这个错误,后来不允许我的应用程序正常工作
这是 product.dart 文件:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:loja_virtual_nnananene/models/item_size.dart';
class Product extends ChangeNotifier {
Product.fromDocument(DocumentSnapshot document) {
id = document.documentID;
name = document['name'];
description = document['description'];
images = List<String>.from(document.data['images'] as List<dynamic>);
// ingore_for_file: Warning: Operand of null-aware operation
sizes = (document.data['sizes'] as List<dynamic> ?? [])
.map((s) => ItemSize.fromMap(s as Map<String, dynamic>))
.toList();
}
String id = "";
String name = "";
String description = "";
List<String> images = [];
List<ItemSize> sizes = [];
ItemSize _selectedSize;
ItemSize get selectedSize => _selectedSize;
set selectedSize(ItemSize value) {
_selectedSize = value;
notifyListeners();
}
}
我在 Product.from... 这是错误:
Non-nullable instance field '_selectedSize' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
这是我的 ItemSize class:
class ItemSize {
ItemSize.fromMap(Map<String, dynamic> map) {
name = map['name'] as String;
price = map['price'] as num;
stock = map['stock'] as int;
}
String name = "";
num price = 0;
int stock = 0;
bool get hasStock => stock > 0;
@override
String toString() {
return 'ItemSize{name: $name, price: $price, stock: $stock}';
}
}
调用主窗口小部件:
class SizeWidget extends StatelessWidget {
const SizeWidget(this.size);
final ItemSize size;
@override
Widget build(BuildContext context) {
final product = context.watch<Product>();
final selected = size == product.selectedSize;
Color color;
if (!size.hasStock)
color = Colors.red.withAlpha(50);
else if (selected)
color = ColorSelect.cprice;
代码第一次尝试获取选中项,当然会为null。我找到的替代方案是...
在 ItemSize class 中,我创建了一个简单的构造函数,所有 null -> ItemSize();
class ItemSize {
ItemSize.fromMap(Map<String, dynamic> map) {
name = map['name'] as String;
price = map['price'] as num;
stock = map['stock'] as int;
}
ItemSize();
String? name;
num? price;
int? stock;
bool get hasStock => stock! > 0;
@override
String toString() {
return 'ItemSize{name: $name, price: $price, stock: $stock}';
}
}
在产品中 class 以这种方式获取。
ItemSize get selectedSize {
if (_selectedSize != null)
return _selectedSize!;
else
return ItemSize();
}