1

我正在尝试对 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',
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                        DataColumn(
                          label: Text(
                            'Delete',
                            style: TextStyle(fontStyle: FontStyle.italic),
                          ),
                        ),
                      ],
                      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设置initStatefalse,但似乎没有奏效。任何帮助表示赞赏!

4

1 回答 1

1

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

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

  // ...

  if(_persons != null) {
        _persons.sort((a, b) =>
                 a.friendlyName.compareTo(b.friendlyName));
        if (!sortAscending) {
             _persons = _persons.toList();
        }
  }
于 2021-02-19T09:36:23.843 回答