Flutters 数据表中的列排序

问题描述

我正在尝试对 Flutter 数据表中的数据进行排序,但无论我尝试什么,它要么什么也不做,要么返回此错误The method 'sort' was called on null. Receiver: null Tried calling: sort(Closure: (VehicleData,VehicleData) => int)。我已经尝试了许多我在网上阅读的选项,但它们似乎都不起作用,所以它必须在我的代码中的某个地方。这是:

import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';
import 'models/vehicle.dart';
import 'screens/vehicle_details_screen.dart';
import 'services/vehicle_api.dart';
import 'screens/edit_screen.dart';
import 'models/vehicle_data_provider.dart';
import 'package:http/http.dart' as http;

class VehicleList extends StatefulWidget {
  @override
  _VehicleList createState() => _VehicleList();
}

class _VehicleList extends State<VehicleList> {
  bool _sortNameAsc = true;
  bool _sortAsc = false;
  int _sortColumnIndex;
  List<VehicleData> _persons;

  _getPosts() async {
    HomePageProvider provider =
        Provider.of<HomePageProvider>(context,listen: false);

    var postsResponse = await fetchVehicles();
    if (postsResponse.isSuccessful) {
      provider.setPostsList(postsResponse.data,notify: false);
    } else {
      provider.mergePostsList(postsResponse.data,notify: false);
    }

    provider.setIsHomePageProcessing(false);
  }

  @override
  void initState() {
    _getPosts();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Consumer<HomePageProvider>(
          builder: (context,vehicleData,child) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.center,mainAxisAlignment: MainAxisAlignment.spaceAround,children: [
                SizedBox(
                  height: 12.0,),Container(
                  decoration: Boxdecoration(
                    color: Colors.grey[300],borderRadius: BorderRadius.all(
                      Radius.circular(12.0),child: SingleChildScrollView(
                    child: DataTable(
                      sortColumnIndex: _sortColumnIndex,sortAscending: _sortAsc,columnSpacing: 30,columns: <DataColumn>[
                        DataColumn(
                          numeric: false,onSort: (columnIndex,sortAscending) {
                            setState(() {
                              if (columnIndex == _sortColumnIndex) {
                                _sortAsc = _sortNameAsc = sortAscending;
                              } else {
                                _sortColumnIndex = columnIndex;
                                _sortAsc = _sortNameAsc;
                              }
                              _persons.sort((a,b) =>
                                  a.friendlyName.compareto(b.friendlyName));
                              if (!sortAscending) {
                                _persons = _persons.toList();
                              }
                            });
                          },label: Text(
                            'Friendly Name',style: TextStyle(fontStyle: FontStyle.italic),DataColumn(
                          label: Text(
                            'Licence Plate',DataColumn(
                          label: Text(
                            'Delete',],rows: List.generate(
                        vehicleData.postsList.length,(index) {
                          VehicleData post = vehicleData.getPostByIndex(index);
                          return DaTarow(
                                  cells: <DataCell>[
                                    DataCell(
                                      Text('${post.friendlyName}'),onTap: () {
                                        Navigator.push(
                                            context,MaterialPageRoute(
                                                builder: (context) =>
                                                    VehicleDetailsScreen(
                                                      color: post.color,friendlyName:
                                                          post.friendlyName,licencePlate:
                                                          post.licencePlate,)));
                                      },DataCell(
                                      Text('${post.licencePlate}'),DataCell(
                                      IconButton(
                                        icon: Icon(Icons.delete),onpressed: () {
                                          vehicleData.deletePost(post);
                                        },);
                        },);
          },);
  }
}

我尝试将 _sortASc 放在 initState 中并将其设置为 false,但似乎没有成功。任何帮助表示赞赏!

解决方法

您的 _persons 列表为空。你可以初始化它。并在排序前检查是否为空。

  List<VehicleData> _persons = List<VehicleData>();

  // ...

  if(_persons != null) {
        _persons.sort((a,b) =>
                 a.friendlyName.compareTo(b.friendlyName));
        if (!sortAscending) {
             _persons = _persons.toList();
        }
  }