So these are my data classes
class Category {
final int? id;
final String name;
final String thumbnail;
Category({this.id, required this.name, required this.thumbnail});
// Convert a Category into map
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'thumbnail': thumbnail,
};
}
// Extract
factory Category.fromMap(Map<String, dynamic> map) {
return Category(
id: map['id'],
name: map['name'],
thumbnail: map['thumbnail'],
);
}
}
class SvgFile {
final int? id;
final int categoryId;
final String filePath;
final String name; // New field for the name of the SVG file
SvgFile({this.id, required this.categoryId, required this.filePath, required this.name});
// Convert SvgFile into map
Map<String, dynamic> toMap() {
return {
'id': id,
'category_id': categoryId,
'file_path': filePath,
'name': name, // Include the name in the map
};
}
// Extract SvgFile from map
factory SvgFile.fromMap(Map<String, dynamic> map) {
return SvgFile(
id: map['id'],
categoryId: map['category_id'],
filePath: map['file_path'],
name: map['name'], // Retrieve the name from the map
);
}
}
Then i have this to manually input data into the sqlite database, i have a db helper as well:
class DataPopulator {
final DatabaseHelper dbHelper = DatabaseHelper.instance;
Future<void> populateData() async {
// List of categories and their associated SVG files with names
final List<Map<String, dynamic>> categoriesWithSvgFiles = [
{
'name': 'Animals',
'thumbnail': 'assets/images/animal.png',
'svgs': [
{'filePath': 'assets/images/animal/weird_animal.svg', 'name': 'Weird Animal'},
{'filePath': 'assets/images/animal/deer.svg', 'name': 'Deer'},
{'filePath': 'assets/images/animal/dino.svg', 'name': 'Dinosaur'},
],
},
];
Future<void> populateData() async {
for (var categoryMap in categoriesWithSvgFiles) {
// Insert category and get the id
int categoryId = await dbHelper.insertCategory(Category(
name: categoryMap['name'],
thumbnail: categoryMap['thumbnail'],
));
// Insert associated SVG files
for (var svg in categoryMap['svgs']) {
await dbHelper.insertSvgFile(SvgFile(
categoryId: categoryId,
filePath: svg['filePath'],
name: svg['name'],
));
}
}
}
// Insert data into the database
for (var categoryData in categoriesWithSvgFiles) {
// Create a Category object
Category category = Category(
name: categoryData['name'],
thumbnail: categoryData['thumbnail'],
);
// Insert the category into the database
int categoryId = await dbHelper.insertCategory(category);
// Insert associated SVG files into the database
for (var svgFileData in categoryData['svgs']) {
SvgFile svgFile = SvgFile(
categoryId: categoryId,
filePath: svgFileData['filePath'],
name: svgFileData['name'],
);
await dbHelper.insertSvgFile(svgFile);
}
}
print("Data population complete");//debug
}
}
void main() async {
DataPopulator dataPopulator = DataPopulator();
await dataPopulator.populateData();
}
When i run my app, it keeps saying “No categories found”
My app has multiple categories, and each category has a list of svgfiles, I want to manually populate the database before opening the application.
this is my main
void main() {
// Initialize the database factory
databaseFactory = databaseFactoryFfi; // Set the database factory
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Category Navigation',
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/see_all_categories': (context) => SeeAllCategoriesPage(),
},
);
}
}