How to view data from SQlite database using expansiontile and listview in flutter

184 Views Asked by At

I'm beginner for flutter. I try to view data from SQlite database using expansiontile and listview. But not succeed.

dbhelper file

static Future<sql.Database> db() async {
    return sql.openDatabase(
      'school.db',
      version: 1,
      onCreate: (sql.Database database, int version) async {
        await createTables(database);
      },
    );
  }
  static Future<List<Map<String,dynamic>>> getTeachers() async {
  final db = await SQLHelper.db();
  return db.rawQuery('select name from teachers where section =?',['AL']);

HomePage file

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

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Map<String, dynamic>> dataset = [];

  _getData() async {
    final data = await dbelper.getTeachers();
    setState(() {
      dataset = data;
    });
    return dataset;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DCS'),
      ),
      body: ListView.builder(
        itemCount: fields.length,
        itemBuilder: (context, i) {
          -_getData();
          return ExpansionTile(
            title: Text(fields[i].title, style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),),
            children: <Widget>[
              Column(
                children: _buildExpandableContent(Fields[i]),
              ),
            ],
          );
        },
      ),
    );
  }

  _buildExpandableContent(Fields fields) {
    List<Widget> columnContent = [];

    for (String content in fields.dataList)
      columnContent.add(
        ListTile(
          title: Text(content, style: TextStyle(fontSize: 12.0,fontWeight: FontWeight.bold),),
          leading: Icon(fields.icon),
        ),
      );
    return columnContent;
  }
}

class Fields {
  final String title;
  List<String> dataList = [];
  final IconData icon;

  Fields(this.title, this.dataList, this.icon);
}

List<Fields> fields = [
  new Fields(
    'Primary Level',
    [], //as assign a List dataset.name of primary level
    Icons.line_style,
  ),
  new Fields(
    'Secodary Level',,
    [], //as assign a List dataset.name of Secondary level
    Icons.color_lens,
  ),
  new Fields(
    'Advance Level',
    [],//as assign a List dataset.name of Advance level
    Icons.ring_volume,
  ),
];

main file

import 'package:dcs_contact/Screens/home_page.dart';
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'School Data',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const HomePage());
  }
}

teachers table detail - id:int, section: string, name: string

I want to store name field of teachers table as a list, to fields.datalist. I don't know how to do it. calling _getData function is correct? I try to call _getData three times changing argument 'PL', 'OL' and 'AL'(levels) only one time and store data to three lists, then assign to datalist of Fields. (like fields.dataList = dataset.name) Please can anyone guide me to do this using this method or any other efficient method? help me. I follow this Flutter :- Is there a way to customize the output for each Expansion Tile in a list builder?

1

There are 1 best solutions below

0
MoosaZK On

There is a syntax error. You are calling the class Fields not the fields list

return ExpansionTile(
        title: Text(fields[i].title, style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),),
        children: <Widget>[
          Column(
           //It is fields not Fields
            children: _buildExpandableContent(fields[i]),
          ),
        ],
      );