Add new product in the database using SQLite

Now it's time to connect the logic with the UI let's first start by adding a new product. so let's open add_product_page.dart file and start adding necessary code for that.

1st step

Let's import tow files here.

import '../models/product.dart';
import '../services/dbhelper.dart';

2nd step

Let's define the fields that we will need to retrieve that given data by the user. Also we need to make an instance of the DBHelper class.

  String name, description;
  double price;
  DbHelper helper;

Don't forget to instantiate the helper on initState.

  @override
  void initState() {
    super.initState();
    helper = DbHelper();
  }

Now let's just get the value of the given name, description and price and store them in our variables using onChanged method in the TextFormField widget.

But for the price we need to covert the string value to double using this expression price = double.parse(value).

Finally when the user tap on the save button we want to save the given values into the database.

    Product product = Product({
            'name': name,
            'description': description,
            'price': price,
        });
    setState(() {
        helper.createProduct(product);
        print('product saved');
    });

So now here is the final code for the add_product_page.dart let's save and see what we have in our browser.

import 'package:flutter/material.dart';
import '../models/product.dart';
import '../services/dbhelper.dart';

class AddProductPage extends StatefulWidget {
  @override
  _AddProductPageState createState() => _AddProductPageState();
}

class _AddProductPageState extends State<AddProductPage> {
  String name, description;
  double price;
  DbHelper helper;

  @override
  void initState() {
    super.initState();
    helper = DbHelper();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Add new product"),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          TextFormField(
            decoration: InputDecoration(
              hintText: 'Enter product name',
              labelText: 'Product name',
            ),
            onChanged: (value) {
              setState(() {
                name = value;
              });
            },
          ),
          SizedBox(height: 16),
          TextFormField(
            maxLines: null,
            decoration: InputDecoration(
              hintText: 'Enter product description',
              labelText: 'Product description',
            ),
            onChanged: (value) {
              setState(() {
                description = value;
              });
            },
          ),
          SizedBox(height: 16),
          TextFormField(
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              hintText: 'Enter product price',
              labelText: 'Product price',
            ),
            onChanged: (value) {
              setState(() {
                price = double.parse(value);
              });
            },
          ),
          SizedBox(height: 16),
          RaisedButton(
            child: Text('Save'),
            onPressed: () {
              Product product = Product({
                'name': name,
                'description': description,
                'price': price,
              });
              setState(() {
                helper.createProduct(product);
                print('product saved');
              });
            },
          ),
        ],
      ),
    );
  }
}

Now if you fill the fields and tap on the save button we imagine that the product has been saved into the the sqlite database. flutter sqflite add new product