Creating products model

After installing and experimenting on the freezed package, now let's create producs model.

First, create the "product.dart" file under src/product/model/product/ with the following code.

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:techamp_flutter_shopping_app/app.dart';

part 'product.freezed.dart';
part 'product.g.dart';

@freezed
abstract class Product with _$Product {
  const factory Product({
    @required int id,
    @required String title,
    @required String image,
    @required double price,
    @required String description,
    @required @CategoryConverter() ProductCategory category,
  }) = _Product;

  factory Product.fromJson(Map<String, dynamic> json) =>
      _$ProductFromJson(json);
}

After this run, the command "pub run build_runner build" on the terminal of the project to generate the product source code. After running it you should see "product.freezed.dart" and "product.g.dart" files next to it where the generated source code is written.

Last updated