首页 > 解决方案 > 颤振长列表给出 NoSuchMethodError

问题描述

我正在构建一个应用程序,其中我在 TabBarView 中使用了一个长列表。但是当它启动这个列表时,它会给出以下错误;

NoSuchMethodError:在 null 上调用了 getter 'length'。

接收方:空

尝试调用:长度

另见:https ://flutter.dev/docs/testing/errors

下面是我的 main.dart;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp(
    contacts : List<String>.generate(10000, (i) {return "Contact $i";}),
));

class MyApp extends StatelessWidget {
  final List<String> contacts;

  const MyApp({Key key, this.contacts}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
          length: 2,
          child: Scaffold(
            appBar: AppBar(
              title: Text('Contacts'),
              bottom: TabBar(tabs: [
                Tab(icon: Icon(Icons.smartphone)),
                Tab(icon: Icon(Icons.face))
              ]),
            ),
            body: TabBarView(
              children: [
                ListView.builder(
                  itemCount: contacts.length,
                  itemBuilder: (context, index){
                    return ListTile(
                        title: Text('Contact ${contacts[index]}')
                    );
                  },
                ),
                Center(child: Text('hello text')),
            ]
            ),
          ),
      ),
    );
  }
}

标签: flutterflutter-listview

解决方案


推荐阅读