每當我嘗試從登錄頁面導航到主頁時,我的應用程式中都會彈出此錯誤。
錯誤截圖
vs 代碼中的除錯控制臺顯示這樣的錯誤。
═════════════ Exception caught by widgets library ═════════════
The following LateError was thrown building HomePage(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#d5a11]], state: _HomePageState#10e51):
LateInitializationError: Field 'items' has not been initialized.
The relevant error-causing widget was
HomePage
lib\main.dart:22
When the exception was thrown, this was the stack
**#0 CatalogModel.items (package:flutter_catalog/models/catalog.dart)
package:flutter_catalog/models/catalog.dart:1
#1 _HomePageState.build and so on.....**
我的 catalog.dart 頁面
class CatalogModel {
static late List<Item> items;
// Get Item by ID
Item getById(int id) =>
items.firstWhere((element) => element.id == id, orElse: null);
// Get Item by position
Item getByPosition(int pos) => items[pos];
}
class Item {
final int id;
final String name;
final String desc;
final num price;
int quantity;
final String color;
final String image;
num get totalPrice => price * quantity;
Item({
required this.id,
required this.name,
required this.desc,
required this.price,
required this.color,
required this.image,
this.quantity = 1,
});
Item copyWith({
int? id,
String? name,
String? desc,
num? price,
String? color,
int? quantity = 1,
String? image,
}) {
return Item(
id: id ?? this.id,
name: name ?? this.name,
desc: desc ?? this.desc,
price: price ?? this.price,
color: color ?? this.color,
image: image ?? this.image,
quantity: quantity ?? this.quantity,
);
}
主頁.dart
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
loadData();
}
loadData() async {
await Future.delayed(Duration(seconds: 2));
final catalogJson =
await rootBundle.loadString("assets/files/catalog.json");
final decodedData = jsonDecode(catalogJson);
var productsData = decodedData["products"];
CatalogModel.items = List.from(productsData)
.map<Item>((item) => Item.fromMap(item))
.toList();
(VxState.store as MyStore).items = CatalogModel.items;
setState(() {});
}
@override
Widget build(BuildContext context) {
final _cart = (VxState.store as MyStore).cart;
final MyStore store = VxState.store;
return Scaffold(
backgroundColor: context.canvasColor,
floatingActionButton: VxBuilder(
mutations: {AddMutation, RemoveMutation},
builder: (ctx,_,status) =>FloatingActionButton(
onPressed: () =>
store.navigator.routeManager.push(Uri.parse(MyRoutes.cartRoute)),
backgroundColor: context.theme.buttonColor,
child: Icon(
CupertinoIcons.cart,
color: Colors.white,
),
).badge(
color: Vx.red500,
size: 22,
count: _cart.items.length,
textStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
)),
),
body: SafeArea(
child: Container(
padding: Vx.m32,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CatalogHeader(),
CupertinoSearchTextField(
onChanged: (value) {
SearchMutation(value);
},
).py12(),
**if (CatalogModel.items !=null && CatalogModel.items.isNotEmpty)**
CatalogList().py16().expand()
else
CircularProgressIndicator().centered().expand(),
],
),
),
));
}
}
購物車.dart
class CartModel {
late CatalogModel _catalog;
final List<int> _itemIds = [];
CatalogModel get catalog => _catalog;
set catalog(CatalogModel newCatalog) {
assert(newCatalog != null);
_catalog = newCatalog;
}
List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
num get totalPrice =>
items.fold(0, (total, current) => total current.price);
}
class AddMutation extends VxMutation<MyStore> {
final Item item;
AddMutation(this.item);
@override
perform() {
store!.cart._itemIds.add(item.id);
}
}
class RemoveMutation extends VxMutation<MyStore> {
final Item item;
RemoveMutation(this.item);
@override
perform() {
store!.cart._itemIds.remove(item.id);
}
}
請幫助我這段代碼,以便當我嘗試從一個頁面導航到另一個頁面時不會彈出上述錯誤(螢屏截圖)。[此外,我嘗試通過洗掉 static late ,@required 關鍵字來修復錯誤,但沒有任何效果,最終出現更多錯誤。請查看代碼并幫助我。]
uj5u.com熱心網友回復:
您正在訪問裝載案例中的專案,獲取它需要一些時間。
Widget build(BuildContext context) {
final _cart = (VxState.store as MyStore).cart; /// here trying to get on 1st frame
使用會更好FutureBuilder。或提供空串列而不是遲到或將其設為 nullabel。
static List<Item> items = [];
檢查FutureBuilder
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526228.html
標籤:扑镖飞镖零安全
