Flutter: How i implement the list in a list? Error: Only static members can be accessed in initializers.dart(implicit_this_reference_in_initializer)

Flutter: How i implement the list in a list? Error: Only static members can be accessed in initializers.dart(implicit_this_reference_in_initializer)

我想要一个包含卖家、买家和卖家产品的应用程序。

我有一个名为 product 的 class,一个名为 seller 的 class 和一个名为 products provider 的 class,其中两个 class 都是被写下来。我把所有 classes 都写在那里。

class 产品:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class Product with ChangeNotifier {
  final String id;
  final String rubric;
  final Color rubricColor;
  final String title;
  final String imagePath;
  final String description;
  final double price;
  bool isFavouriteProduct;

  Product({
    @required this.id,
    @required this.title,
    @required this.rubric,
    @required this.rubricColor,
    this.imagePath = 'assets/images/defaultpicture.png',
    this.description,
    @required this.price,
    this.isFavouritProduct = false,
  });
}

Class 卖家:

import 'package:flutter/material.dart';
import '**/providers/product.dart';

class Seller with ChangeNotifier {
  final String id;
  final String title;
  bool isFavourite;

  final List<Product> items;


  Seller({this.id, this.isFavourite, this.items, this.title});
}

Class 产品提供商:

import 'package:flutter/material.dart';
import '***/providers/seller.dart';
import './product.dart';

class ProductsProvider with ChangeNotifier{

  List<Seller> _profile = [
    Seller(id: '1', isFavourite: false, title: 'Blabla-store', items: _items)
  ];

  List<Product> _items = [
  Product(
    id: 'a1',
    title: 'blablastore',
    rubric: '',
    rubricColor: Colors.orange,
    description: 'blablabla',
    imagePath: 'assets/images/***.jpeg',
    price: 200000.00,
  ),
  Product(
    id: 'a2',
    title: 'blablastore2',
    rubric: '',
    rubricColor: Colors.blue,
    description:
        'blablabla,
    imagePath: 'assets/images/nah.jpg',
    price: 49.99,
  ),
  Product(
    id: 'a3',
    title: 'Mir fällt nix ein',
    rubric: '',
    rubricColor: Colors.redAccent,
    description: 'weiß nicht',
    price: 22.22,
  )
];

  List<Product> get items{
    return [..._items];
  }

  Product findById(String id){
    return _items.firstWhere((product)=> product.id == id);
  }

  void addProduct(){
    notifyListeners();
  }
}

但是在 ProductsProvider class 中,我通过项目 属性 -> "Only static members can be accessed in initializers.dart (implicit_this_reference_in_initializer)"

得到了这个错误

所以我记下了"static",错误就这样解决了:

import 'package:flutter/material.dart';
import '***/providers/seller.dart';
import './product.dart';

class ProductsProvider with ChangeNotifier{

  List<Seller> _profile = [
    Seller(id: '1', isFavourite: false, title: 'Blabla-store', items: _items)
  ];

  static List<Product> _items = [
  Product(
    id: 'a1',
    title: 'blablastore',
    rubric: '',
    rubricColor: Colors.orange,
    description: 'blablabla',
    imagePath: 'assets/images/turkitchdoener.jpeg',
    price: 200000.00,
  ),
  Product(
    id: 'a2',
    title: 'blablastore2',
    rubric: '',
    rubricColor: Colors.blue,
    description:
        'blablabla,
    imagePath: 'assets/images/nah.jpg',
    price: 49.99,
  ),
  Product(
    id: 'a3',
    title: 'Mir fällt nix ein',
    rubric: '',
    rubricColor: Colors.redAccent,
    description: 'weiß nicht',
    price: 22.22,
  )
];

  List<Product> get items{
    return [..._items];
  }

  Product findById(String id){
    return _items.firstWhere((product)=> product.id == id);
  }

  void addProduct(){
    notifyListeners();
  }
}

但我担心通过静态更改,卖家无法再添加新项目、编辑项目或删除项目。我错了吗?

我认为你在错误变量(_profile) 的字体中添加了static,它应该在_items 前面,因为你在其他变量(如_profile)的初始化中使用了_items。

静态变量是指 class 变量,这意味着无论您创建多少对象 class 但该变量将在所有对象之间共享。您可以执行所有操作,例如添加项目、删除项目或编辑。

此外,您不能使用对象访问 _items 变量。您可以使用 class 名称访问它。

查看有关 Static

的更多信息