颤抖的长列表给出NoSuchMethodError

时间:2020-08-08 02:59:35

标签: flutter flutter-listview

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

NoSuchMethodError:在空值上调用了吸气剂“长度”。

收件人:空

尝试通话:时长

另请参阅: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')),
            ]
            ),
          ),
      ),
    );
  }
}

0 个答案:

没有答案