[英]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.