I'm doing the example in Searchview flutter https://github.com/MageshPandian20/Flutter-SearchView but I wanted to make a change to the ChildItem class which has a
final String name attribute;
and in my case I have a Product class so I had to modify the code in that sense. When I run the application I get the following error :
I/flutter (18897): The following NoSuchMethodError was thrown building IndexFragment(dirty, state: I/flutter (18897): _SearchListState#4f3d3): I/flutter (18897): The method 'map' was called on null. I/flutter (18897): Receiver: null I/flutter (18897): Tried calling: map(Closure: (Product) => ChildItem)
I don't understand why I can get that error. In a moment of tests and errors run the application in debug mode and everything worked but a little slow because of the way I ran it... I think I have omitted a small detail but when I run the application I get the error mentioned above. I hope you can help me. Thank you.
*PS: When running the application in debug mode check that the Product list was not empty as well as the attributes of each item in the list.
My Code:
import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
import 'package:graphqllapp/data/product_data.dart';
import 'package:graphqllapp/modules/product_presenter.dart';
class IndexFragment extends StatefulWidget {
IndexFragment({ Key key }) : super(key: key);
@override
_SearchListState createState() => _SearchListState();
}
class _SearchListState extends State<IndexFragment> implements ProductListView
{
Widget appBarTitle = new Text("Portada", style: new TextStyle(color: Colors.white),);
Icon actionIcon = new Icon(Icons.search, color: Colors.white,);
final key = new GlobalKey<ScaffoldState>();
final TextEditingController _searchQuery = new TextEditingController();
List<Product> _list;
bool isSearching;
String _searchText = "";
ProductListPresenter _presenter;
_SearchListState() {
_presenter = new ProductListPresenter(this);
_presenter.loadProducts();
_searchQuery.addListener(() {
if (_searchQuery.text.isEmpty) {
setState(() {
isSearching = false;
_searchText = "";
});
}
else {
setState(() {
isSearching = true;
_searchText = _searchQuery.text;
});
}
});
}
void init() {
_presenter.loadProducts();
}
@override
void initState() {
super.initState();
init();
isSearching = false;
}
@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[ new Expanded(
child: new SizedBox(
child: new Carousel(
images: [
new ExactAssetImage('images/glutamina.jpg'),
new ExactAssetImage('images/frasco1.jpg'),
new ExactAssetImage('images/frasco.jpg')]
)
),flex: 2),
new Expanded(
child: new Column(children: <Widget>[
new IconButton(icon: actionIcon, onPressed: () {
setState(() {
if (this.actionIcon.icon == Icons.search) {
this.actionIcon = new Icon(Icons.close, color: Colors.white,);
this.appBarTitle = new TextField(
controller: _searchQuery,
style: new TextStyle(
color: Colors.white,
),
decoration: new InputDecoration(
prefixIcon: new Icon(Icons.search, color: Colors.white),
hintText: "Search...",
hintStyle: new TextStyle(color: Colors.white)
),
);
_handleSearchStart();
} else {
_handleSearchEnd();
}
});
},),new ListView(
padding: new EdgeInsets.symmetric(vertical: 8.0),
children: isSearching ? _buildSearchList() : _buildList(),
)] ),flex : 4)]);
}
List<ChildItem> _buildList() {
return _list.map((product) => new ChildItem(product)).toList();
}
List<ChildItem> _buildSearchList() {
if (_searchText.isEmpty) {
return _list.map((product) => new ChildItem(product)).toList();
} else {
List<Product> _searchList = List();
for (int i = 0; i < _list.length; i++) {
Product product = _list.elementAt(i);
if (product.name.toLowerCase().contains(_searchText.toLowerCase())) {
_searchList.add(product);
}
}
return _searchList.map((product) => new ChildItem(product)).toList();
}
}
void _handleSearchStart() {
setState(() {
isSearching = true;
});
}
void _handleSearchEnd() {
setState(() {
this.actionIcon = new Icon(Icons.search, color: Colors.white,);
this.appBarTitle =
new Text("Search Sample", style: new TextStyle(color: Colors.white),);
isSearching = false;
_searchQuery.clear();
});
}
@override
void onLoadProductsError(String msg) {
// TODO: implement onLoadProductsError
}
@override
void onLoadProductsFinish(List<Product> products) {
// TODO: implement onLoadProductsFinish
_list = products;
}
}
class ChildItem extends StatelessWidget {
final Product product;
ChildItem(this.product);
@override
Widget build(BuildContext context) {
return new ListTile(
leading: new CircleAvatar(
child: Image.memory(product.mainImage),
backgroundColor: Colors.transparent,
),
title: new Text(product.name, style : new TextStyle(fontWeight: FontWeight.bold)),
subtitle: new Text(product.description) ,
isThreeLine: true,
);
}
}
Product Class:
import 'dart:async';
import 'dart:typed_data';
import 'dart:convert';
class Product {
int id;
String name;
String description;
Uint8List mainImage;
Uint8List firstImage;
Uint8List secondImage;
Product({this.id,this.name,this.description,this.mainImage,this.firstImage,this.secondImage});
Product.fromMap(Map<String,dynamic> map)
:id = map["id"],
name = map["name"],
description = map["description"],
mainImage = base64.decode(map["main_image"]),
firstImage = base64.decode(map["first_image"]),
secondImage = base64.decode(map["second_image"]);
}
MockProductRepository class :
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:graphqllapp/data/product_data.dart';
class MockProductRepository implements ProductRepository {
@override
Future<List<Product>> fetchProducts() async {
// TODO: implement fetchUsers
String data = await rootBundle.loadString("mockdata/data.json");
var jsonResult = json.decode(data);
return (jsonResult['products'] as List).map((p)=> Product.fromMap(p)).toList();
}
}
Presenter class:
import 'package:graphqllapp/data/product_data.dart';
import 'package:graphqllapp/dependency_injection.dart';
abstract class ProductListView {
void onLoadProductsFinish(List<Product> users);
void onLoadProductsError(String msg);
}
class ProductListPresenter {
ProductListView _view;
ProductRepository _repository;
ProductListPresenter(this._view){
_repository = Injector().productRepository;
}
void loadProducts(){
_repository.fetchProducts()
.then((v)=>_view.onLoadProductsFinish(v))
.catchError((onError)=>_view.onLoadProductsError("Error to get users: $onError"));
}
}
The error seems to be caused by _list
being null
before its initialization is done via onLoadProductsFinish
. Simply declare your _list
with an empty ([]
) list and it should work.
List<Product> _list = [];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With