'How to load data into the database and get it back

When I open the screen by "ID", I get the title and data from "dummy.dart" in accordance with the "exercise_model.dart" model, which fall into the "Slider", in which, if necessary, I change them and they immediately get into the database. And when loading the screen, so that they are loaded. I'm new to this business. Please tell me how to save the data to the database and read them from there when loading the screen. Thanks!

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

import '../dummy.dart';
import '../helpers/db_helper.dart';
import '../models/exercise_model.dart';

class ExerciseDetailScreen extends StatefulWidget {
  static const routeName = '/detail-exercise';

  @override
  State<ExerciseDetailScreen> createState() => _ExerciseDetailScreenState();
}

class _ExerciseDetailScreenState extends State<ExerciseDetailScreen> {
  List<Exercise> itemDb;

  double weightSliderValue;

  // (weightSliderValue == null) ? 50 :

  // Future fetchAndSetExercises() async {
  //     final dataList = await DBHelper.getData('user_exercises');
  //     itemDb = dataList
  //         .map((item) => Exercise(
  //               id: item['id'],
  //               title: item['title'],
  //               weight: item['weight'],
  //             ))
  //         .toList();
  //   }

  @override
  Widget build(BuildContext context) {
    var routeArgs =
        ModalRoute.of(context).settings.arguments as Map<dynamic, dynamic>;

    final exerciseId = routeArgs['id'];
    final exerciseTitle = routeArgs['title'];
    // var weightSliderValue = routeArgs['weight'];

    return Scaffold(
      backgroundColor: Colors.grey[900],
      appBar: AppBar(
        title: Text(
          exerciseTitle,
          style: Theme.of(context).textTheme.bodyText2,
        ),
      ),
      body: Column(
        children: [
          Container(
            margin: const EdgeInsets.all(20),
            alignment: Alignment.center,
            child: Text(
              exerciseTitle,
              style: Theme.of(context).textTheme.bodyText2,
            ),
          ),
          Card(
            elevation: 7,
            color: Colors.teal,
            child: ListTile(
              title: Text(
                'Вес $weightSliderValue',
                style: Theme.of(context).textTheme.headline1,
              ),
            ),
          ),
          Slider(
            value: weightSliderValue,
            max: 200,
            divisions: 400,
            onChanged: (double value) {
              setState(
                () {
                  weightSliderValue = value;
                  DBHelper.insert('user_exercises', {
                    'id': exerciseId,
                    'title': exerciseTitle,
                    'weight': weightSliderValue,
                  });
                },
              );
            },
          ),
        ],
      ),
    );
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source