I want an app with the sellers, buyers and the products of the sellers.
I have a class with the name product, a class with the name seller and a class with the name products provider, in which both classes are written down. I write all the classes down there.
class Product:
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 Seller:
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 ProductsProvider:
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();
}
}
But in the ProductsProvider class I get this error by items property -> "Only static members can be accessed in initializers.dart (implicit_this_reference_in_initializer)"
So i write down "static" and the error is fixed like this:
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();
}
}
but i am afraid that by changing with static, the seller can no longer add new items, edit items or delete items. Am I wrong?
I think you add static in font of wrong variable(_profile) it should be in front of _items because you are using _items in the initialisation of other variable's like _profile.
Static variable mean class variable, means that whatever amount of objects you create of that class but that variable will be shared between all that object. You can do all the things like additem, deleteitem or edit.
Moreover, you can't access _items variable using object. You can access that using class name.
checkout more about Static