I’m developing a category page, I fetch category data from my backend service and then render the category list, it works well.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
but throw an Exception: flutter: Another exception was thrown: Incorrect use of ParentDataWidget.
Here is my flutter code:
import 'package:flutter/material.dart';
import 'package:talkie/model/category_data.dart';
import 'package:talkie/pages/home/home_search_bar.dart';
import '../../http/category_api.dart';
class CategoryPage2 extends StatefulWidget {
const CategoryPage2({super.key});
@override
State<CategoryPage2> createState() => _CategoryPage2State();
}
class _CategoryPage2State extends State<CategoryPage2> {
CategoryApi _categoryApi = CategoryApi();
late List<CategoryData> _categories;
@override
void initState() {
super.initState();
fetchCategories();
}
Future<void> fetchCategories() async {
try {
final res = await _categoryApi.getCategories();
setState(() {
if (res == null) {
_categories = [];
} else {
_categories = (res as List<dynamic>)
.map((item) => CategoryData.fromJson(item))
.toList();
}
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: HomeSearchBar(),
),
body: Row(
children: [
Expanded(
child: _buildCategries()
),
// Expanded(child: _buildAlbums())
],
),
);
}
Widget _buildCategries() {
return ListView.builder(
itemCount: _categories.length,
itemBuilder: (context, index) {
CategoryData categoryData = _categories[index];
return ListTile(
title: Text(categoryData.name!),
);
});
}
// Widget _buildAlbums() {
// return Text("albums");
// }
}
It’s my code, please check and let me know WHY.
New contributor