Flutter:获取json固定数据并按字母顺序排序 [英] Flutter: Get json fixed data and ordered-by alphabetically
本文介绍了Flutter:获取json固定数据并按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从 json API 中获取数据.并且想要显示我的应用程序哪个数据的类别等于1.我可以通过调用 category-1 url链接 完成此操作,但是我一次需要通过链接获取所有数据然后分成很多部分.
I get data from a json API. And want to show my app which data's category is equal to 1. I could have done this by call category-1 url link but I need all data by link at a time and then divided into many parts.
我正在使用if(mydata[index].category==1)
然后返回.但是我认为这是错误的划分方式.告诉我输出错误.
I am using if(mydata[index].category==1)
then return. But I think it's a wrong way to divide it. It's show me wrong output.
我还想按字母顺序对数据进行排序.
And I want to also sorting this data by alphabetically.
这是我的代码-
import 'package:boimarket/booksdescription.dart';
import 'package:boimarket/model/model.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:draggable_scrollbar/draggable_scrollbar.dart';
class StoryBooksCategory extends StatefulWidget {
final ScrollController controller;
final List<Book> storybooks;
StoryBooksCategory({Key key, @required this.controller, this.storybooks})
: super(key: key);
@override
_StoryBooksCategoryState createState() => _StoryBooksCategoryState();
}
class _StoryBooksCategoryState extends State<StoryBooksCategory> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
var _height = MediaQuery.of(context).size.height;
var _width = MediaQuery.of(context).size.width;
final Color _whiteCream = Color.fromRGBO(250, 245, 228, 1);
final Color _darkBlue = Color.fromRGBO(0, 68, 69, 1);
return Align(
alignment: Alignment.topCenter,
child: Container(
width: _width / 1.1,
child: FutureBuilder(
future: fetchBooks(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
LinearProgressIndicator(
backgroundColor: _whiteCream,
),
Text("Loading"),
],
);
} else if (snapshot.hasData) {
var mydata = snapshot.data;
return DraggableScrollbar.rrect(
controller: widget.controller,
backgroundColor: _darkBlue,
child: ListView.builder(
controller: widget.controller,
scrollDirection: Axis.vertical,
itemCount: mydata.length,
itemBuilder: (context, index) {
if (mydata[index].category == 1)
return GestureDetector(
onTap: () {
Route route = MaterialPageRoute(
builder: (context) =>
BookDescription(storyBooksValue: mydata[index]),
);
Navigator.push(context, route);
},
child: Padding(
padding: const EdgeInsets.only(
left: 10.0, right: 10.0, top: 20.0),
child: Container(
width: _width / 1.1,
height: _height / 4,
decoration: BoxDecoration(
color: _whiteCream,
borderRadius: BorderRadius.all(
Radius.circular(5.0),
),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 2,
spreadRadius: 2,
offset: Offset(2.0, 2.0),
)
]),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
flex: 4,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Card(
elevation: 10.0,
child: ClipRRect(
borderRadius:
BorderRadius.circular(5.0),
child: FadeInImage.assetNetwork(
fadeOutCurve: Curves.easeInCubic,
placeholder:
'assets/images/bookshelf.jpg',
image: mydata[index].imgUrl == null
? 'assets/images/bookshelf.jpg'
: mydata[index].imgUrl,
fit: BoxFit.cover,
),
),
),
),
),
Flexible(
flex: 6,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
Text(
"বইয়ের নামঃ ",
style: TextStyle(
fontWeight: FontWeight.bold),
),
Flexible(
child: Text(
"${mydata[index].name}",
overflow: TextOverflow.fade,
),
),
],
),
Row(
children: <Widget>[
Text(
"লেখকঃ ",
style: TextStyle(
fontWeight: FontWeight.bold),
),
Flexible(
child: Text(
"${mydata[index].author}",
overflow: TextOverflow.ellipsis,
),
),
],
),
Row(
children: <Widget>[
Text(
"বইয়ের ধরণঃ ",
style: TextStyle(
fontWeight: FontWeight.bold),
),
Flexible(
child: Text(
"${mydata[index].genreClass}",
overflow: TextOverflow.ellipsis,
),
),
],
),
],
),
),
],
),
),
),
);
},
),
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(Icons.error_outline),
Text("Somthing Went to wrong"),
],
);
}
},
),
),
);
}
}
推荐答案
使用where子句过滤出您想要的书-例如:
Use a where clause to filter out the books you want - for example:
var books = await fetchBooks();
var cat1books = books.where((b) => b.category == 1).toList();
您可以在这里使用它:
var mydata = snapshot.data.where((b) => b.category == 1).toList();
mydata.sort((a, b) => a.name.compareTo(b.name));
这篇关于Flutter:获取json固定数据并按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文