[英]Why can't I create flutter <dart> dynamic category list?
我正在嘗試通過 Android 工作室中的 flutter 制作動態類別列表,但即使沒有錯誤,我的代碼也不起作用。 為什么?
我遵循幾年前的培訓計划,雖然培訓師使用的是舊版本的flutter,但我能夠以某種方式糾正他所做的每個項目中的錯誤。
此項目中唯一的錯誤是 main_screen.dart class 中的 getCategoryWidget function 中的 flatButton,因為 flatButton 已被棄用
這是我的主要內容。dart
import 'package:flutter/material.dart';
import 'package:http_demo/screens/Main_Screen.dart';
void main() {
runApp(HttpApp());
}
class HttpApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MainScreen(),
);
}
}
類別.dart
class Category {
int? id;
String? categoryName;
String? seoUrl;
Category(this.id, this.categoryName, this.seoUrl);
Category.fromJson(Map json) {
id = json["id"];
categoryName = json["categoryName"];
seoUrl = json["seoUrl"];
}
Map tojson() {
return {"id": id, "categoryName": categoryName, "seoUrl": seoUrl};
}
}
category_api.dart
import 'package:http/http.dart' as http;
class CategoryApi{
static Future getCategories(){
return http.get(Uri.parse("http://10.0.2.2:3000/categories"));
}
}
product_api.dart
import 'package:http/http.dart' as http;
class ProductApi {
static Future getProducts() {
return http.get(Uri.parse("http://10.0.2.2:3000/products"));
}
static Future getProductsByCategoryId(int categoryId) {
return http.get(Uri.parse("http://10.0.2.2:3000/products?categoryId=$categoryId"));
}
}
main_screen.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http_demo/data/data.api/category_api.dart';
import '../models/category.dart';
class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainScreenState();
}
}
class MainScreenState extends State {
List<Category> categories = List<Category>.empty();
List<Widget> categoryWidgets = [];
@override
void initState() {
getCategoriesFromApi();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Alışveriş sistemi",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.blueGrey,
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: categoryWidgets,
),
)
],
),
),
);
}
void getCategoriesFromApi() {
CategoryApi.getCategories().then((response) {
setState(() {
Iterable list = json.decode(response.body);
categories =
list.map((category) => Category.fromJson(category)).toList();
getCategoryWidgets();
});
});
}
List<Widget> getCategoryWidgets() {
for (int i = 0; i < categories.length; i++) {
categoryWidgets.add(getCategoryWidget(categories[i]));
}
return categoryWidgets;
}
Widget getCategoryWidget(Category category) {
return TextButton(
onPressed: () {
},
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: Colors.lightGreenAccent),
),
),
),
child: Text(
category.categoryName!,
style: TextStyle(color: Colors.blueGrey),
),
);
}
}
我是 flutter 和 dart 的新手,我哪里出錯了?
用最少的信息我認為你的問題從這里開始:
/// Make the following adjustments assuming your api returns results.
Future<void> getCategoriesFromApi() async {
await CategoryApi.getCategories().then((response) {
Iterable list = json.decode(response.body); //I don't think this is right. Try to print(json.decode(response.body)); and check
List<Category> categories = list.map((category) => Category.fromJson(category)).toList();// Don't make List<Category> global
getCategoryWidgets(categories); //pass as parameter
});
}
//Inside getCategoryWidgets(List<Categories> categories):
void getCategoryWidgets(List<Categories> categories) {
List<Widget> list = [];
for (int i = 0; i < categories.length; i++) {
list.add(getCategoryWidget(categories[i]));
}
setState(()=> categoryWidgets = list);
//No return
}
注意:測試看看它是否有效。 沒有保證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.