如何解决我如何在flutter中使用dart或http在数据中使用数组列表
这是我的模型文件如何访问类别数组列表,类别名称数据:
class CategoryModelData {
List<Categories> categories;
String bannerImage;
String status;
String message;
CategoryModelData(
{this.categories,this.bannerImage,this.status,this.message});
CategoryModelData.fromJson(Map<String,dynamic> json) {
if (json['categories'] != null) {
categories = new List<Categories>();
json['categories'].forEach((v) {
categories.add(new Categories.fromJson(v));
});
}
bannerImage = json['banner_image'];
status = json['status'];
message = json['message'];
}
Map<String,dynamic> toJson() {
final Map<String,dynamic> data = new Map<String,dynamic>();
if (this.categories != null) {
data['categories'] = this.categories.map((v) => v.toJson()).toList();
}
data['banner_image'] = this.bannerImage;
data['status'] = this.status;
data['message'] = this.message;
return data;
}
}
class Categories {
String categoryId;
String categoryName;
String parentId;
List<Child> child;
Categories({this.categoryId,this.categoryName,this.parentId,this.child});
Categories.fromJson(Map<String,dynamic> json) {
categoryId = json['category_id'];
categoryName = json['category_name'];
parentId = json['parent_id'];
if (json['child'] != null) {
child = new List<Child>();
json['child'].forEach((v) {
child.add(new Child.fromJson(v));
});
}
}
Map<String,dynamic>();
data['category_id'] = this.categoryId;
data['category_name'] = this.categoryName;
data['parent_id'] = this.parentId;
if (this.child != null) {
data['child'] = this.child.map((v) => v.toJson()).toList();
}
return data;
}
}
class Child {
String categoryId;
String categoryName;
String parentId;
Child({this.categoryId,this.parentId});
Child.fromJson(Map<String,dynamic> json) {
categoryId = json['category_id'];
categoryName = json['category_name'];
parentId = json['parent_id'];
}
Map<String,dynamic>();
data['category_id'] = this.categoryId;
data['category_name'] = this.categoryName;
data['parent_id'] = this.parentId;
return data;
}
}
import 'package:appointment_flutter/category/Rfid/category_model.dart'; //here is my main code from this icall my api
import 'package:appointment_flutter/category/Rfid/fabric_repository.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'action_data.dart';
class CategoryScreen extends StatefulWidget {
CategoryScreenState createState() => CategoryScreenState();
}
class CategoryScreenState extends State<CategoryScreen> {
List<CategoryModelData> categorysData = [];
List<String> categoryName = [];
String catagoryNameList;
bool isLoading = true;
@override
void initState() {
getCategory().then((value) {
if (value != null) {
categorysData = value;
if (categorysData.length != 0 && categorysData != null) {
categoryName = [];
categorysData.forEach((element) {
//categoryName.add(element.categories[0].categoryName.toString());
var categoryName;
categoryName.add(element.categories
.indexWhere((element1) => element1.categoryName.toString()));
});
}
}
setState(() {
isLoading = false;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
print("Categoers:" + categoryName.toString());
return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
"Category",style: TextStyle(color: Colors.black54),)),actions: [
ActionData(),],backgroundColor: Colors.White,),body: SingleChildScrollView(
scrollDirection: Axis.vertical,child: Container(
width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,child: ListView.builder(
itemCount: 20,itemBuilder: (BuildContext context,int position) {
return Container(
width: MediaQuery.of(context).size.width,height: 200,color: Colors.red,margin: EdgeInsets.all(1),child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [
Text("dfhed"),Image.asset("assets/images/more.png")
],);
})),);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。