Cuando ejecuto esta aplicación, obtengo este error: *NoSuchMethodError' The method '[]' was called on null. Receiver: null. Tried Calling: . Me sale cuando ejecuto la app en mi celular. Me muestra directamente el error en rojo. Probé modificarlo de diferentes maneras pero no funcionó y de las soluciones que encontré en Internet tampoco me sirvieron. Soy nuevo en flutter y en dart, estoy tratando de aprender, sabes como podría solucionarlo para que la app pueda correr sin problemas? Gracias!
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=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX&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,
);
}
}