Home > Net >  The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was
The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was

Time:08-11

I'm trying to connect the article page to the search page body content, hope to be clear, but I get the error

The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null. Receiver: null Tried calling: data

Anyone can solve it? Thanks in advance

class _SearchPageState extends State<SearchPage> {
  List<Article> _searchedPost = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TextField(
          style: const TextStyle(color: Colors.white),
          decoration: const InputDecoration(
              hintText: 'Search Post',
              hintStyle: TextStyle(color: Colors.white),
              border: InputBorder.none),
          onChanged: (val) {
            setState(() {
              _searchedPost =
                  widget.posts.where((el) => el.title.contains(val)).toList();
            });
          },
        ),
      ),

      /// Body
      body: _searchedPost.isEmpty
          ? Center(
              child: Text(
                'Nessun articolo disponibile',
                textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.headline6,
              ),
            )
          : ListView.builder(
              itemCount: _searchedPost.length,
              itemBuilder: (context, i) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Card(
                      margin: const EdgeInsets.all(10),
                      elevation: 5,
                      shadowColor: Colors.black26,
                      color: Colors.white,
                      child: InkWell(
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              _searchedPost[i].urlImage == null
                                  ? const Text("Nessuna immagine caricata")
                                  : Image.network(
                                      _searchedPost[i].urlImage,
                                      width: double.infinity,
                                      height: 220,
                                      fit: BoxFit.cover,
                                    ),
                              // Title article
                              Column(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 16, top: 16, bottom: 16),
                                    child: Row(
                                      children: [
                                        Expanded(
                                          child: Text(
                                            _searchedPost[i].title,
                                            maxLines: 3,
                                            overflow: TextOverflow.clip,
                                            softWrap: true,
                                            style: const TextStyle(
                                              fontSize: 18,
                                              fontWeight: FontWeight.w600,
                                              fontFamily: "Raleway",
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => ArticlePage(
                                data: snapshot.data?[i],
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                );
              },
            ),
    );
  }
}

CodePudding user response:

Your data has never assigned. It is better to make it nullable and check while using it.

YourDataType? data;

There is no snapshoot variable I can see. You might want to pass _searchedPost[i]

Try like this on your on Tap, replace it on OnTap

onTap: () {
  if (_searchedPost[i] != null) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ArticlePage(
          data: _searchedPost[i],
        ),
      ),
    );
  }
},

And on ArticlePage

class ArticlePage extends StatelessWidget {
  final Article data; 
  const ArticlePage({
   Key? key,
   required this.data,
  })

And

Image.network( data.urlImage,
Text(data.title
  • Related