【问题标题】:Flutter: Get json fixed data and ordered-by alphabeticallyFlutter:获取 json 固定数据并按字母顺序排序
【发布时间】:2020-11-26 14:34:21
【问题描述】:

我从json API 获取数据。并想向我的应用显示哪个数据的类别等于 1。我可以通过调用 category-1 url link 来完成此操作,但我需要一次链接的所有数据然后分成很多部分。

我正在使用if(mydata[index].category==1)然后返回。但我认为这是一种错误的划分方式。它显示错误的输出。

我还想按字母顺序对这些数据进行排序。

这是我的代码-

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"),
                ],
              );
            }
          },
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: json api sorting flutter where-clause


    【解决方案1】:

    使用 where 子句过滤出您想要的书籍 - 例如:

    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));
    

    【讨论】:

    • 类别没问题,但如何按字母顺序排序。 var storybooks = snapshot.data.where((b) => b.category == 1).toList(); var mydata = storybooks.sort((a, b) => a.name.compareTo(b.name));它不工作。
    • sort 返回null - 所以你已经将mydata 设为null!而是使用:storybooks.sort(...); 然后var mydata = storybooks;
    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多