我该如何解决这个错误? 'NoSuchMethodError' 方法 '[]' 在 null 上被调用。接收器:空。尝试调用:[]("title")

时间:2021-06-07 23:22:38

标签: flutter dart

当我运行这个应用程序时,我得到这个错误“NoSuchMethodError”方法“[]”被调用为空。接收器:空。尝试调用:。 我尝试了不同的方法,但没有奏效,我也没有在互联网上找到解决方案,我是 flutter 新手,我正在努力学习,你知道该由谁来做吗?谢谢!

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


class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
//Step 3
  _HomeScreenState() {
    _filter.addListener(() {
      if (_filter.text.isEmpty) {
        setState(() {
          _searchText = "";
          filteredNames = titulos;
        });
      } else {
        setState(() {
          _searchText = _filter.text;
        });
      }
    });
  }

//Step 1
  final TextEditingController _filter = new TextEditingController();
  final dio = new Dio(); // for http requests
  String _searchText = "";
  List titulos = new List(); // titulos we get from API
  List filteredNames = new List(); // names filtered by search text
  Icon _searchIcon = new Icon(Icons.search);
  Widget _appBarTitle = new Text('Search Example');

  //step 2.1
  void _getNames() async {
    final response = await dio.get(
        'https://api.themoviedb.org/3/movie/popular?api_key=0e685fd77fb3d76874a3ac26e0db8a4b&language=en-US&page=1');
    print(response.data);
    List tempList = new List();
    for (int i = 0; i < response.data.length; i++) {
      tempList.add(response.data[i]);
    }
    setState(() {
      titulos = tempList;
      filteredNames = titulos;
    });
  }

//Step 2.2
  void _searchPressed() {
    setState(() {
      if (this._searchIcon.icon == Icons.search) {
        this._searchIcon = new Icon(Icons.close);
        this._appBarTitle = new TextField(
          controller: _filter,
          decoration: new InputDecoration(
              prefixIcon: new Icon(Icons.search), hintText: 'Search...'),
        );
      } else {
        this._searchIcon = new Icon(Icons.search);
        this._appBarTitle = new Text('Search a movie');
        filteredNames = titulos;
        _filter.clear();
      }
    });
  }

  //Step 4
  Widget _buildList() {
    if (!(_searchText.isEmpty)) {
      List tempList = new List();
      for (int i = 0; i < filteredNames.length; i++) {
        if (filteredNames[i]['title']
            .toLowerCase()
            .contains(_searchText.toLowerCase())) {
          tempList.add(filteredNames[i]);
        }
      }
      filteredNames = tempList;
    }
    return ListView.builder(
      itemCount: titulos == null ? 0 : filteredNames.length,
      itemBuilder: (BuildContext context, int index) {
        return new ListTile(
          title: Text(filteredNames[index]['title']),
          onTap: () => print(filteredNames[index]['title']),
        );
      },
    );
  }

  //STep6
  Widget _buildBar(BuildContext context) {
    return new AppBar(
      centerTitle: true,
      title: _appBarTitle,
      leading: new IconButton(
        icon: _searchIcon,
        onPressed: _searchPressed,
      ),
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildBar(context),
      body: Container(
        child: _buildList(),
      ),
      resizeToAvoidBottomPadding: false,
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我是 Dart 的新手,这是我在这里的第一个答案,但也许您想要获取的索引为空。例如,如果 myVariable 为空,则无法访问 myVariable[1]myVariable['propertyname']
我认为相反,您可以使用 myVariable?['propertyname'],如果 null 本身为空,它将返回 myVariable,因此不会引发错误。 或者,您可以检查您的程序流程并确保在使用时相关变量不为空。

(如果回答不好,我很抱歉,我是新来的)
(请对我的话持保留态度。我正在尽力回答,但我肯定可能是错的。)