NoSuchMethodError: getter length was called on null

NoSuchMethodError: getter length was called on null

我能够创建 SQFLite 数据库和 运行 查询,但是,当我尝试使用 listview.separated 小部件显示结果时收到 noSuchMethodError。回顾了其他类似的堆栈溢出主题,但不是运气。代码错误 - 创建局部变量?

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

final dbHelper = DatabaseHelper();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<Breeds> allBreedsList;

    return MaterialApp(
        title: 'My 1st App',
        home: Scaffold(
            appBar: AppBar(
              title: Text('Pets, Pets, Pets'),
            ),
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
              RaisedButton(
                  child: Text(
                    'query',
                    style: TextStyle(fontSize: 20),
                  ),
                  onPressed: () {
                    print("Yeah, it works!");
                    {
                      _pullAllbreed();
                    }
                    Expanded(
                        child: ListView.separated(
                      itemBuilder: (context, index) {
                        return ListTile(
                          leading: Text("${allBreedsList[index]}"),
                          title: Text("${allBreedsList[index].name}"),
                        );
                      },
                      separatorBuilder: (context, index) => Divider(),
                      itemCount: allBreedsList.length,
                    ));
                  }),
            ]))));
  }
}

void _pullAllbreed() async {
  final allBreedsList = await dbHelper.breedList();
  print('querying all breeds now, please stand by');
  allBreedsList.forEach((row) => print(row));
  int breedCount = allBreedsList.length;
  print(breedCount);
}

您可以复制粘贴 运行 下面的完整代码
第 1 步:从 RaisedButton
移出 ListView.separated 第 2 步:从 build
移出 List<Breeds> allBreedsList 第 3 步:MyApp 使用 StatefulWidget 并在 _pullAllbreed();

之后调用 setState(() {});
onPressed: () {
        print("Yeah, it works!");
        _pullAllbreed();
        setState(() {});
      }),

工作演示

完整代码

import 'package:flutter/material.dart';

class DatabaseHelper {
  Future<List<Breeds>> breedList() {
    return Future.value(
        [Breeds(name: "a"), Breeds(name: "b"), Breeds(name: "c")]);
  }
}

class Breeds {
  String name;
  Breeds({this.name});
}

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

final dbHelper = DatabaseHelper();

List<Breeds> allBreedsList = [];

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'My 1st App',
        home: Scaffold(
            appBar: AppBar(
              title: Text('Pets, Pets, Pets'),
            ),
            body: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                  RaisedButton(
                      child: Text(
                        'query',
                        style: TextStyle(fontSize: 20),
                      ),
                      onPressed: () {
                        print("Yeah, it works!");
                        _pullAllbreed();
                        setState(() {});
                      }),
                  Expanded(
                      child: ListView.separated(
                    itemBuilder: (context, index) {
                      return ListTile(
                        leading: Text("${allBreedsList[index]}"),
                        title: Text("${allBreedsList[index].name}"),
                      );
                    },
                    separatorBuilder: (context, index) => Divider(),
                    itemCount: allBreedsList.length,
                  ))
                ]))));
  }
}

void _pullAllbreed() async {
  allBreedsList = await dbHelper.breedList();
  print('querying all breeds now, please stand by');
  allBreedsList.forEach((row) => print(row));
  int breedCount = allBreedsList.length;
  print(breedCount);
}