【问题标题】:Flutter: How to sort ListView (data from Json) in decreasing order or alphabetically?Flutter:如何按降序或字母顺序对 ListView(来自 Json 的数据)进行排序?
【发布时间】:2020-05-30 03:46:14
【问题描述】:

这是 JSON 文件

[
  {
    "id": 1,
    "country": "United States",
    "population": 328200000
  },
  {
    "id": 2,
    "country": "Germany",
    "population": 83020000
  },
  {
    "id": 3,
    "country": "United Kingdom",
    "population": 66650000
  }
]

我希望在按下RaisedButton 后,它会按顺序排序('Country Name' => 减少;'Country Population' => 按字母顺序)(image preview)。该怎么做?

这是主文件:

import 'dart:convert';
import 'dart:core';
import 'package:ask/country.dart';
import 'package:ask/country_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Country> _country = [];

  @override
  void initState() {
    CountryServices.getCountry().then((value) {
      setState(() {
        _country = value;
      });
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    return new MaterialApp(
        home: Scaffold(
            appBar: AppBar(title: Text('Country')),
            body: Container(
                child: Column(children: <Widget>[
                  Row(children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: RaisedButton(
                            child: Text('Country Name'),
                            onPressed: () {}, // Sort alphabetically
                          )),
                      Expanded(
                          flex: 1,
                          child: RaisedButton(
                            child: Text('Country Population'),
                            onPressed: () {}, // Sort in decreasing order
                          ))]),
                   Container(
                      child: ListView.builder(
                        shrinkWrap: true,
                        itemCount: _country.length,
                        itemBuilder: (context, index) {
                          return _listCountry(index);
                        }))
            ]))));
  }

  _listCountry(index) {
    Country country = _country[index];
    return Row(
      children: <Widget>[
        Expanded(
            flex: 1,
            child: Text(
              country.country,
              textAlign: TextAlign.center,
            )),
        Expanded(
            flex: 1,
            child: Text('${country.population}', textAlign: TextAlign.center)),
      ],
    );
  }
}


【问题讨论】:

    标签: sorting listview flutter dart


    【解决方案1】:

    This官方文档会提供如何排序的信息。

    试试下面的。

    CountryServices.getCountry().then((value) {
    
    
        setState(() {
            _country = value;
            _country.sort((a, b) => a.population.compareTo(b.population));
        });
    });
    

    注意:以上代码未经测试。您可能需要稍作调整。

    【讨论】:

    • 你能告诉我把这段代码放在哪里吗?我尝试添加此here 但出现错误,我认为我在某处错了
    • 这是错误详情link
    • 成功了,谢谢!但是我如何从onPressedRaisedButton 调用这个?
    • 太棒了。试一试。如果您无法解决难题,请在 StackOverflow 上发布另一个问题。不过不应该要求。 :)
    • 无论如何,谢谢,我创建了一个新问题here,这次我想将排序方法应用于ToggleButtons,如果您能再次帮助我,那就太好了:D
    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 2018-07-21
    • 2013-11-09
    • 2012-09-14
    • 2021-12-20
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多